import 'dart:io';

import 'package:gametime_server/infrastructure/postgres/postgres_database.dart';
import 'package:test/test.dart';

void main() {
  test('PostgreSQL migrations create the sync-ready schema', () async {
    final databaseUrl = Platform.environment['TEST_DATABASE_URL'];
    if (databaseUrl == null || databaseUrl.trim().isEmpty) {
      markTestSkipped(
        'Set TEST_DATABASE_URL to run PostgreSQL migration tests.',
      );
      return;
    }

    final connection = await PostgresConnectionFactory.fromDatabaseUrl(
      databaseUrl,
    ).open();
    addTearDown(() => connection.close());

    await PostgresMigrator(connection).applyMigrations();

    final result = await connection.execute('''
      SELECT table_name
      FROM information_schema.tables
      WHERE table_schema = 'public'
        AND table_name IN (
          'users',
          'auth_sessions',
          'synced_resources',
          'shares',
          'share_recipients'
        )
    ''');
    final tableNames = result.map((row) => row[0] as String).toSet();

    expect(
      tableNames,
      containsAll({
        'users',
        'auth_sessions',
        'synced_resources',
        'shares',
        'share_recipients',
      }),
    );
  });
}
