import "@testing-library/jest-dom/vitest";
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { EligibilityGauge } from "@/components/agnet/finance/EligibilityGauge";
import { ApprovalPathStepper } from "@/components/agnet/finance/ApprovalPathStepper";
import { BankRoutingList } from "@/components/agnet/finance/BankRoutingList";
import { DocumentChecklist } from "@/components/agnet/finance/DocumentChecklist";
import { CaseHeaderMetrics } from "@/components/agnet/finance/CaseHeaderMetrics";
import type { FinanceViewModel } from "@/lib/farmer-dashboard";

const mockFinance: FinanceViewModel = {
  purpose: "working_capital",
  amount: "85000000",
  existingLoans: "12000000",
  monthlyIncome: "9500000",
  repaymentHistory: "good",
  caseId: "UZB-2024-05-000789",
  status: "under_review",
  stages: [
    { label: "Case Created", completed: true, timestamp: "2024-05-23T10:15:00.000Z" },
    { label: "Eligibility Check", completed: true, timestamp: "2024-05-23T11:02:00.000Z" },
    { label: "Document Review", completed: true, timestamp: "2024-05-23T13:45:00.000Z" },
    { label: "Routed to Institution", completed: true, timestamp: "2024-05-23T14:30:00.000Z" },
    { label: "Institution Assessment", completed: false },
    { label: "Credit Committee", completed: false },
    { label: "Final Decision", completed: false },
  ],
  eligibilityScore: 78,
  eligibilityLabel: "High",
  routedInstitution: {
    name: "Agrobank (UZ)",
    priority: "High",
    isBestMatch: true,
  },
  sla: {
    remainingHours: 18,
    totalHours: 48,
    dueAt: "2024-05-25T18:30:00.000Z",
  },
  bankMatches: [
    { name: "Agrobank (UZ)", score: 92, status: "routed" },
    { name: "Xalq Banki", score: 84, status: "view" },
  ],
  documents: [
    { name: "ID / Passport", status: "verified" },
    { name: "Financial Statement", status: "pending" },
  ],
};

describe("EligibilityGauge", () => {
  it("shows eligibility score out of 100", () => {
    render(
      <EligibilityGauge score={78} label="Eligible" locale="en" />,
    );
    expect(screen.getByRole("group", { name: /Eligibility: 78 of 100/i })).toBeInTheDocument();
    expect(screen.getByText("Eligible")).toBeInTheDocument();
  });
});

describe("CaseHeaderMetrics", () => {
  it("renders case id and institution", () => {
    render(
      <CaseHeaderMetrics finance={mockFinance} gisVerified locale="en" />,
    );
    expect(screen.getByText("UZB-2024-05-000789")).toBeInTheDocument();
    expect(screen.getByText("Agrobank (UZ)")).toBeInTheDocument();
    expect(screen.getByText("GIS Verified")).toBeInTheDocument();
  });
});

describe("BankRoutingList", () => {
  it("renders bank matches with routed badge", () => {
    render(
      <BankRoutingList matches={mockFinance.bankMatches!} locale="en" />,
    );
    expect(screen.getByText("Agrobank (UZ)")).toBeInTheDocument();
    expect(screen.getByText("Xalq Banki")).toBeInTheDocument();
    expect(screen.getAllByText("Routed").length).toBeGreaterThanOrEqual(1);
  });
});

describe("DocumentChecklist", () => {
  it("lists verified and pending documents", () => {
    render(
      <DocumentChecklist documents={mockFinance.documents!} locale="en" />,
    );
    expect(screen.getByText("ID / Passport")).toBeInTheDocument();
    expect(screen.getByText("Financial Statement")).toBeInTheDocument();
  });
});

describe("ApprovalPathStepper", () => {
  it("reflects finance stages with active step", () => {
    render(
      <ApprovalPathStepper stages={mockFinance.stages} locale="en" />,
    );
    expect(screen.getByText("Case Created")).toBeInTheDocument();
    expect(screen.getByText("Institution Assessment")).toBeInTheDocument();
    const nav = screen.getByRole("navigation", { name: "Approval Path" });
    expect(nav.querySelector("[aria-current='step']")).toBeTruthy();
  });
});
