import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
import {
  authenticateViaApply,
  completeAccountStep,
  loginAsDemoFarmer,
  uniqueTestPhone,
} from "../helpers/wizard";

async function expectNoCriticalViolations(page: import("@playwright/test").Page, context: string) {
  const results = await new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa"]).analyze();
  const critical = results.violations.filter(
    (v) => v.impact === "critical" || v.impact === "serious",
  );
  expect(critical, `${context} accessibility violations`).toEqual([]);
}

/** G5 gate: dashboard routes — critical impact only (per UI_UX_LIBRARY_PLAN §13). */
async function expectNoAxeCritical(page: import("@playwright/test").Page, context: string) {
  const results = await new AxeBuilder({ page }).withTags(["wcag2a", "wcag2aa"]).analyze();
  const critical = results.violations.filter((v) => v.impact === "critical");
  expect(critical, `${context} critical accessibility violations`).toEqual([]);
}

test.describe("Farmer flow accessibility (WCAG 2.1 AA)", () => {
  test("apply page has no critical/serious axe violations", async ({ page }) => {
    await page.goto("/farmers/apply");
    await expect(page.locator("#apply-phone")).toBeVisible();
    await expectNoCriticalViolations(page, "apply");
  });

  test("login page has no critical/serious axe violations", async ({ page }) => {
    await page.goto("/farmers/login");
    await expect(page.locator("#login-phone")).toBeVisible();
    await expectNoCriticalViolations(page, "login");
  });

  test("onboarding account step has no critical/serious axe violations", async ({ page }) => {
    const phone = uniqueTestPhone();
    await authenticateViaApply(page, phone);
    await expect(page.locator("h2")).toContainText(/account|Hisob|аккаунт/i);
    await expectNoCriticalViolations(page, "onboarding account");
  });

  test("onboarding consent step has no critical/serious axe violations", async ({ page }) => {
    const phone = uniqueTestPhone();
    await authenticateViaApply(page, phone);
    await completeAccountStep(page, phone);

    await expect(page.locator("h2")).toContainText(/Consent|Rozilik|Согласие/);
    await expectNoCriticalViolations(page, "onboarding consent");
  });

  test("unauthenticated dashboard redirect target (login) is accessible", async ({ page }) => {
    await page.goto("/farmers/dashboard");
    await page.waitForURL("**/farmers/login**");
    await expect(page.locator("#login-phone")).toBeVisible();
    await expectNoCriticalViolations(page, "dashboard redirect login");
  });

  test.describe("dashboard modules (demo personas)", () => {
    test("overview hub has no critical axe violations", async ({ page }) => {
      await loginAsDemoFarmer(page, "+998901110001");
      await page.setViewportSize({ width: 1440, height: 900 });
      await page.goto("/farmers/dashboard");
      await expect(page.getByTestId("dashboard-sidebar")).toBeVisible();
      await expectNoAxeCritical(page, "dashboard overview");
    });

    test("identity module has no critical axe violations", async ({ page }) => {
      await loginAsDemoFarmer(page, "+998901110001");
      await page.setViewportSize({ width: 1440, height: 900 });
      await page.goto("/farmers/dashboard/identity");
      await expect(page.getByTestId("identity-module")).toBeVisible({ timeout: 15_000 });
      await expectNoAxeCritical(page, "dashboard identity");
    });

    test("finance module has no critical axe violations", async ({ page }) => {
      await loginAsDemoFarmer(page, "+998901110002");
      await page.setViewportSize({ width: 1440, height: 900 });
      await page.goto("/farmers/dashboard/finance");
      await expect(page.getByText(/Case ID|ID дела/)).toBeVisible({ timeout: 15_000 });
      await expectNoAxeCritical(page, "dashboard finance");
    });

    test("inputs module has no critical axe violations", async ({ page }) => {
      await loginAsDemoFarmer(page, "+998901110003");
      await page.setViewportSize({ width: 1440, height: 900 });
      await page.goto("/farmers/dashboard/inputs");
      await expect(page.getByTestId("inputs-module")).toBeVisible({ timeout: 15_000 });
      await expectNoAxeCritical(page, "dashboard inputs");
    });
  });
});
