import "@testing-library/jest-dom/vitest";
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { mapDashboardResponse } from "@/lib/farmer-dashboard";
import { DASHBOARD_MOCKUP_PERSONAS } from "@/lib/fixtures/dashboard-mockup-personas";
import IdentityModule from "@/components/farmer/modules/IdentityModule";
import ComplianceChecklist from "@/components/agnet/identity/ComplianceChecklist";
import { LocaleProvider } from "@/lib/i18n/locale-provider";

vi.mock("next/image", () => ({
  default: ({ alt }: { alt: string; fill?: boolean }) => (
    // eslint-disable-next-line @next/next/no-img-element
    <img alt={alt} />
  ),
}));

vi.mock("recharts", async () => {
  const React = await import("react");
  return {
    ResponsiveContainer: ({ children }: { children: React.ReactNode }) => (
      <div data-testid="mock-chart">{children}</div>
    ),
    BarChart: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
    Bar: () => null,
    XAxis: () => null,
    YAxis: () => null,
    CartesianGrid: () => null,
    Tooltip: () => null,
  };
});

function renderWithLocale(ui: React.ReactElement) {
  return render(<LocaleProvider>{ui}</LocaleProvider>);
}

describe("IdentityModule", () => {
  const identity = mapDashboardResponse(DASHBOARD_MOCKUP_PERSONAS.islomjon).identity!;

  it("renders v2 layout sections for islomjon persona", () => {
    renderWithLocale(<IdentityModule data={identity} />);

    expect(screen.getByTestId("identity-module")).toBeInTheDocument();
    expect(screen.getByTestId("identity-gis-card")).toBeInTheDocument();
    expect(screen.getByTestId("identity-ekyc-card")).toBeInTheDocument();
    expect(screen.getByTestId("identity-farm-profile-card")).toBeInTheDocument();
    expect(screen.getByTestId("identity-land-evidence-card")).toBeInTheDocument();
    expect(screen.getByTestId("identity-yield-chart-card")).toBeInTheDocument();
    expect(screen.getByTestId("identity-compliance-card")).toBeInTheDocument();
    expect(screen.getByText("AGID-FMR-00142")).toBeInTheDocument();
    expect(screen.getAllByText("Islomjon Abdullaev").length).toBeGreaterThan(0);
  });

  it("shows empty state when data is null", () => {
    renderWithLocale(<IdentityModule data={null} />);
    expect(screen.getByTestId("identity-empty-state")).toBeInTheDocument();
  });
});

describe("ComplianceChecklist", () => {
  it("shows compliant banner when all items pass", () => {
    const items = [
      { key: "gis", label: "GIS boundary verified", passed: true },
      { key: "ekyc", label: "eKYC identity verified", passed: true },
    ];

    renderWithLocale(<ComplianceChecklist items={items} />);
    expect(screen.getByTestId("identity-compliance-banner")).toBeInTheDocument();
    expect(screen.getByTestId("identity-compliance-banner")).toHaveTextContent(/Muvofiq|Compliant|Соответствует/);
  });
});
