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 15 – weather', () => {
  let harness: StepHarness;

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

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

  it('executes on Weather page in mock wizard and applies weather config from JSON-backed controls', async () => {
    await harness.navigateToFixtureStep(15);

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

    const sidebarWeather = await page!.textContent(
      '#wizard-sidebar-link-set-weather',
    );
    expect(sidebarWeather).toContain('Weather');

    const bodyText = await page!.textContent('body');
    expect(bodyText).toMatch(/Weather Mode|Event weather/i);

    const config = {
      weatherType: '2',
      temperature: 650,
    };

    const result = await harness.executeStep(15, config);

    expect(result.success).toBe(true);
    expect(result.error).toBeUndefined();

    const weatherSelect = page!
      .locator(IRACING_SELECTORS.steps.weatherType)
      .first();
    const weatherSelectCount = await weatherSelect.count();

    if (weatherSelectCount > 0) {
      const selectedWeatherValue =
        (await weatherSelect.getAttribute('value')) ??
        (await weatherSelect.textContent().catch(() => null));
      expect(
        (selectedWeatherValue ?? '').toLowerCase(),
      ).toMatch(/static|forecast|timeline|2/);
    } else {
      const radioGroup = page!.locator('[role="radiogroup"] input[type="radio"]').first();
      const radioCount = await radioGroup.count();
      expect(radioCount).toBeGreaterThan(0);
    }

    const tempSlider = page!
      .locator(IRACING_SELECTORS.steps.temperature)
      .first();
    const tempExists = await tempSlider.count();

    if (tempExists > 0) {
      const tempValue =
        (await tempSlider.getAttribute('data-value')) ??
        (await tempSlider.inputValue().catch(() => null));
      expect(tempValue).toBe(String(config.temperature));
    }
  });
});