import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import {
  PlaywrightAutomationAdapter,
  FixtureServer,
} from 'core/automation/infrastructure//automation';
import { StepId } from 'apps/companion/main/automation/domain/value-objects/StepId';
import { PinoLogAdapter } from 'core/automation/infrastructure//logging/PinoLogAdapter';
import { IRACING_SELECTORS } from 'core/automation/infrastructure//automation/dom/IRacingSelectors';
import { executeStepWithAutoNavigationGuard } from '../support/AutoNavGuard';

describe('Workflow – hosted session autonav slice (fixture-backed, real stack)', () => {
  let server: FixtureServer;
  let adapter: PlaywrightAutomationAdapter;
  let baseUrl: string;

  beforeAll(async () => {
    server = new FixtureServer();
    const info = await server.start();
    baseUrl = info.url;

    const logger = new PinoLogAdapter();

    adapter = new PlaywrightAutomationAdapter(
      {
        headless: true,
        timeout: 15_000,
        baseUrl,
        mode: 'real',
        userDataDir: '',
      },
      logger,
    );
    const result = await adapter.connect(false);
    expect(result.success).toBe(true);
    expect(adapter.isConnected()).toBe(true);
  });

  afterAll(async () => {
    await adapter.disconnect();
    await server.stop();
  });

  async function expectStepOnContainer(
    expectedContainer: keyof typeof IRACING_SELECTORS.wizard.stepContainers,
  ) {
    const page = adapter.getPage();
    expect(page).not.toBeNull();
    const selector = IRACING_SELECTORS.wizard.stepContainers[expectedContainer];
    const container = page!.locator(selector).first();
    await container.waitFor({ state: 'attached', timeout: 10_000 });
    expect(await container.count()).toBeGreaterThan(0);
  }

  it(
    'navigates via autonav across representative steps (1 → 3 → 7 → 9 → 13 → 17)',
    async () => {
      await adapter.navigateToPage(server.getFixtureUrl(1));
      const step1Result = await executeStepWithAutoNavigationGuard(adapter, 1, {});
      expect(step1Result.success).toBe(true);

      await adapter.navigateToPage(server.getFixtureUrl(3));
      const step3Result = await executeStepWithAutoNavigationGuard(adapter, 3, {
        sessionName: 'Autonav workflow session',
        password: 'autonav',
        description: 'Fixture-backed autonav slice',
      });
      expect(step3Result.success).toBe(true);
      await expectStepOnContainer('raceInformation');

      await adapter.navigateToPage(server.getFixtureUrl(7));
      const step7Result = await executeStepWithAutoNavigationGuard(adapter, 7, {
        practice: 10,
        qualify: 10,
        race: 20,
      });
      expect(step7Result.success).toBe(true);
      await expectStepOnContainer('timeLimit');

      await adapter.navigateToPage(server.getFixtureUrl(9));
      const step9Result = await executeStepWithAutoNavigationGuard(adapter, 9, {
        carSearch: 'Acura ARX-06',
      });
      expect(step9Result.success).toBe(true);
      await expectStepOnContainer('cars');

      await adapter.navigateToPage(server.getFixtureUrl(13));
      const step13Result = await executeStepWithAutoNavigationGuard(adapter, 13, {
        trackSearch: 'Spa',
      });
      expect(step13Result.success).toBe(true);
      await expectStepOnContainer('trackOptions');

      await adapter.navigateToPage(server.getFixtureUrl(17));
      const step17Result = await executeStepWithAutoNavigationGuard(adapter, 17, {
        trackState: 'medium',
      });
      expect(step17Result.success).toBe(true);
      await expectStepOnContainer('raceOptions');
    },
    120_000,
  );
});