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 { IRACING_SELECTORS } from 'core/automation/infrastructure//automation/dom/IRacingSelectors';
import { PinoLogAdapter } from 'core/automation/infrastructure//logging/PinoLogAdapter';

describe('Workflow – steps 7–9 cars flow (fixture-backed, real stack)', () => {
  let adapter: PlaywrightAutomationAdapter;
  let server: FixtureServer;
  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: 8000,
        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();
  });

  it(
    'executes time limits, cars, and add car in sequence using fixtures and leaves DOM-backed state',
    async () => {
      await adapter.navigateToPage(server.getFixtureUrl(7));
      const step7Result = await adapter.executeStep(StepId.create(7), {
        practice: 10,
        qualify: 10,
        race: 20,
      });
      expect(step7Result.success).toBe(true);

      const page = adapter.getPage();
      expect(page).not.toBeNull();

      const raceSlider = page!
        .locator(IRACING_SELECTORS.steps.race)
        .first();
      const raceSliderValue =
        (await raceSlider.getAttribute('data-value')) ??
        (await raceSlider.inputValue().catch(() => null));
      expect(raceSliderValue).toBe('20');

      await adapter.navigateToPage(server.getFixtureUrl(8));
      const step8Result = await adapter.executeStep(StepId.create(8), {});
      expect(step8Result.success).toBe(true);

      const carsContainer = page!
        .locator(IRACING_SELECTORS.wizard.stepContainers.cars)
        .first();
      expect(await carsContainer.count()).toBeGreaterThan(0);

      const addCarButton = page!
        .locator(IRACING_SELECTORS.steps.addCarButton)
        .first();
      expect(await addCarButton.count()).toBeGreaterThan(0);

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

      const carsTable = page!
        .locator('#select-car-set-cars table.table.table-striped')
        .first();
      expect(await carsTable.count()).toBeGreaterThan(0);

      const acuraCell = carsTable.locator('tbody tr td >> text=Acura ARX-06 GTP');
      expect(await acuraCell.count()).toBeGreaterThan(0);
    },
  );
});