// dart format width=80
// ignore_for_file: unused_local_variable, unused_import
import 'package:drift/drift.dart';
import 'package:drift_dev/api/migrations_native.dart';
import 'package:household_keeper/core/database/database.dart';
import 'package:flutter_test/flutter_test.dart';
import 'generated/schema.dart';

import 'generated/schema_v1.dart' as v1;
import 'generated/schema_v2.dart' as v2;
import 'generated/schema_v3.dart' as v3;

void main() {
  driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
  late SchemaVerifier verifier;

  setUpAll(() {
    verifier = SchemaVerifier(GeneratedHelper());
  });

  group('simple database migrations', () {
    // These simple tests verify all possible schema updates with a simple (no
    // data) migration. This is a quick way to ensure that written database
    // migrations properly alter the schema.
    //
    // Note: since AppDatabase.schemaVersion == 3, all migration paths
    // end at v3. We only test migrations to the current schema version.
    final fromVersions = [1, 2];
    for (final fromVersion in fromVersions) {
      test('from $fromVersion to 3', () async {
        final schema = await verifier.schemaAt(fromVersion);
        final db = AppDatabase(schema.newConnection());
        await verifier.migrateAndValidate(db, 3);
        await db.close();
      });
    }
  });

  test('migration from v2 to v3 does not corrupt data', () async {
    // The v2 -> v3 migration adds the isActive column (default true).
    // Existing tasks should remain and be accessible with isActive = true.
    await verifier.testWithDataIntegrity(
      oldVersion: 2,
      newVersion: 3,
      createOld: v2.DatabaseAtV2.new,
      createNew: v3.DatabaseAtV3.new,
      openTestedDatabase: AppDatabase.new,
      createItems: (batch, oldDb) {},
      validateItems: (newDb) async {},
    );
  });
}
