import 'package:sqflite/sqflite.dart';

class DatabaseMigrationHandler {
  static Future<void> migrate(
      Database db, int oldVersion, int newVersion) async {
    if (oldVersion < 2) {
      // Add indexes for better performance
      await db.execute(
          'CREATE INDEX IF NOT EXISTS idx_products_name_search ON products(name)');
      await db.execute(
          'CREATE INDEX IF NOT EXISTS idx_products_sku_search ON products(sku)');
    }

    if (oldVersion < 3) {
      // Add full text search support
      await db.execute(
          'CREATE VIRTUAL TABLE products_fts USING fts5(name, sku, description, content=products, content_rowid=rowid)');
      await db.execute(
          'INSERT INTO products_fts SELECT name, sku, description FROM products');
    }

    if (oldVersion < 4) {
      // Add sync tracking
      await db.execute('ALTER TABLE products ADD COLUMN last_sync_at TEXT');
      await db.execute(
          'ALTER TABLE products ADD COLUMN sync_version INTEGER DEFAULT 1');
    }
  }
}
