import { afterEach, beforeEach, describe, expect, it } from 'vitest';

/**
 * Unit test to verify persistence mode configuration
 * This test ensures that the correct persistence modules are loaded
 * based on the GRIDPILOT_API_PERSISTENCE environment variable
 */
describe('Persistence Mode Verification', () => {
  const originalEnv = { ...process.env };

  beforeEach(() => {
    // Reset modules before each test
    vi.resetModules();
  });

  afterEach(() => {
    // Restore original environment
    process.env = { ...originalEnv };
  });

  it('should load postgres persistence when GRIDPILOT_API_PERSISTENCE=postgres', async () => {
    process.env.GRIDPILOT_API_PERSISTENCE = 'postgres';
    process.env.DATABASE_URL = 'postgres://user:pass@localhost:5432/test';

    const { getApiPersistence } = await import('../env');
    const persistence = getApiPersistence();
    
    expect(persistence).toBe('postgres');
  });

  it('should load inmemory persistence when GRIDPILOT_API_PERSISTENCE=inmemory', async () => {
    process.env.GRIDPILOT_API_PERSISTENCE = 'inmemory';
    delete process.env.DATABASE_URL;

    const { getApiPersistence } = await import('../env');
    const persistence = getApiPersistence();
    
    expect(persistence).toBe('inmemory');
  });

  it('should default to inmemory in test environment without DATABASE_URL', async () => {
    process.env.NODE_ENV = 'test';
    delete process.env.GRIDPILOT_API_PERSISTENCE;
    delete process.env.DATABASE_URL;

    const { getApiPersistence } = await import('../env');
    const persistence = getApiPersistence();
    
    expect(persistence).toBe('inmemory');
  });

  it('should verify all persistence modules have both inmemory and postgres alternatives', async () => {
    const fs = require('fs');
    const path = require('path');

    // Dynamically discover all persistence modules by scanning directories
    const inMemoryDir = path.join(__dirname, 'inmemory');
    const postgresDir = path.join(__dirname, 'postgres');

    // Get all module files from inmemory directory
    const inMemoryModules = fs.readdirSync(inMemoryDir)
      .filter((file: string) => file.endsWith('PersistenceModule.ts'))
      .map((file: string) => file.replace('.ts', ''));

    // Get all module files from postgres directory
    const postgresModules = fs.readdirSync(postgresDir)
      .filter((file: string) => file.endsWith('PersistenceModule.ts'))
      .map((file: string) => file.replace('.ts', ''));

    // Verify we have the same number of modules in both directories
    expect(inMemoryModules.length).toBe(postgresModules.length);
    expect(inMemoryModules.length).toBeGreaterThan(0);

    // Verify each in-memory module has a corresponding postgres module
    for (const inMemoryModule of inMemoryModules) {
      // Convert InMemoryX to PostgresX
      const postgresModule = inMemoryModule.replace('InMemory', 'Postgres');
      expect(postgresModules).toContain(postgresModule);

      // Verify both files exist
      expect(fs.existsSync(path.join(inMemoryDir, `${inMemoryModule}.ts`))).toBe(true);
      expect(fs.existsSync(path.join(postgresDir, `${postgresModule}.ts`))).toBe(true);
    }

    // Log the discovered modules for verification
    console.log('Discovered persistence modules:', {
      count: inMemoryModules.length,
      modules: inMemoryModules.map((m: string) => m.replace('InMemory', ''))
    });
  });

  it('should verify that postgres persistence modules exist and are properly structured', async () => {
    const fs = require('fs');
    const path = require('path');

    // Verify that PostgresRacingPersistenceModule exists and exports TypeORM repositories
    const postgresRacingModulePath = path.join(__dirname, 'postgres', 'PostgresRacingPersistenceModule.ts');
    expect(fs.existsSync(postgresRacingModulePath)).toBe(true);

    // Read the module to verify it uses TypeORM
    const moduleContent = fs.readFileSync(postgresRacingModulePath, 'utf-8');
    expect(moduleContent).toContain('TypeOrmModule');
    expect(moduleContent).toContain('TypeOrmDriverRepository');
    expect(moduleContent).toContain('TypeOrmLeagueRepository');
  });

  it('should verify that in-memory modules exist for comparison', async () => {
    const fs = require('fs');
    const path = require('path');

    // Verify that InMemoryRacingPersistenceModule exists
    const inMemoryRacingModulePath = path.join(__dirname, 'inmemory', 'InMemoryRacingPersistenceModule.ts');
    expect(fs.existsSync(inMemoryRacingModulePath)).toBe(true);

    // Read the module to verify it uses InMemory repositories
    const moduleContent = fs.readFileSync(inMemoryRacingModulePath, 'utf-8');
    expect(moduleContent).toContain('InMemoryDriverRepository');
    expect(moduleContent).toContain('InMemoryLeagueRepository');
    expect(moduleContent).not.toContain('TypeOrmModule');
  });

  it('should verify docker-compose.test.yml is configured for PostgreSQL', async () => {
    const fs = require('fs');
    const path = require('path');

    const dockerComposePath = path.join(__dirname, '..', '..', '..', '..', 'docker-compose.test.yml');
    expect(fs.existsSync(dockerComposePath)).toBe(true);

    const content = fs.readFileSync(dockerComposePath, 'utf-8');

    // Verify PostgreSQL service exists
    expect(content).toContain('db:');
    expect(content).toContain('postgres:15-alpine');

    // Verify API service uses postgres persistence
    expect(content).toContain('GRIDPILOT_API_PERSISTENCE=postgres');
    expect(content).toContain('DATABASE_URL=');

    // Verify database connection environment variables
    expect(content).toContain('POSTGRES_DB=gridpilot_test');
    expect(content).toContain('POSTGRES_USER=gridpilot_test_user');
    expect(content).toContain('POSTGRES_PASSWORD=gridpilot_test_pass');
  });
});