import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import type { StepHarness } from '../support/StepHarness';
import { createStepHarness } from '../support/StepHarness';
import { IRACING_SELECTORS } from 'core/automation/infrastructure//automation/dom/IRacingSelectors';

describe('Step 3 – race information', () => {
  let harness: StepHarness;

  beforeEach(async () => {
    harness = await createStepHarness();
  });

  afterEach(async () => {
    await harness.dispose();
  });

  it('fills race information on Race Information page and persists values in form fields', async () => {
    await harness.navigateToFixtureStep(3);
  
    const page = harness.adapter.getPage();
    expect(page).not.toBeNull();
  
    const sidebarRaceInfo = await page!
      .locator(IRACING_SELECTORS.wizard.sidebarLinks.raceInformation)
      .first()
      .innerText();
    expect(sidebarRaceInfo).toMatch(/Race Information/i);
  
    const config = {
      sessionName: 'GridPilot E2E Session',
      password: 'secret',
      description: 'Step 3 race information E2E',
    };
  
    const result = await harness.executeStep(3, config);
  
    expect(result.success).toBe(true);
    expect(result.error).toBeUndefined();
  
    const sessionNameInput = page!
      .locator(IRACING_SELECTORS.steps.sessionName)
      .first();
    const passwordInput = page!
      .locator(IRACING_SELECTORS.steps.password)
      .first();
    const descriptionInput = page!
      .locator(IRACING_SELECTORS.steps.description)
      .first();
  
    const sessionNameValue = await sessionNameInput.inputValue();
    const passwordValue = await passwordInput.inputValue();
    const descriptionValue = await descriptionInput.inputValue();
  
    expect(sessionNameValue).toBe(config.sessionName);
    expect(passwordValue).toBe(config.password);
    expect(descriptionValue).toBe(config.description);
  
    const footerText = await page!.textContent('.wizard-footer');
    expect(footerText).toMatch(/Server Details|Admins/i);
  });
});