"use client";

import * as React from "react";

import { Example } from "@/components/design-system/example";
import { Section } from "@/components/design-system/section";
import { BranchComparison, GoodsReturnsReport, InternalIssueReport, PurchaseAgeing, TransferNote, TransferReports, type TransferRecord } from "@/components/medora/inventory-reports";
import { PrintButton } from "@/components/medora/print";

const BRANCHES = ["Westlands Main", "Kilimani", "Nyali", "Milimani"];

const TRANSFERS: TransferRecord[] = [
  {
    ref: "TRF-0231",
    date: "18 Oct 2026",
    from: "Westlands Main",
    to: "Kilimani",
    sentBy: "Grace Wanjiru",
    receivedBy: "Peter Kamau",
    receivedOn: "19 Oct 2026",
    note: "For the weekend clinic",
    lines: [
      { item: "Panadol 500 mg", unit: "strips", sent: 100, received: 100, unitCost: 32 },
      { item: "Amoxil 500 mg, 21s", unit: "packs", sent: 20, received: 20, unitCost: 210 },
    ],
  },
  {
    ref: "TRF-0234",
    date: "19 Oct 2026",
    from: "Nyali",
    to: "Westlands Main",
    sentBy: "Ali Hassan",
    receivedBy: "Grace Wanjiru",
    receivedOn: "20 Oct 2026",
    shortNote: "Carton torn; 6 tubes missing on arrival, rider informed",
    lines: [
      { item: "Nivea Soft cream 200 ml", unit: "tubes", sent: 24, received: 18, unitCost: 455 },
      { item: "Calamine lotion 100 ml", unit: "bottles", sent: 12, received: 12, unitCost: 160 },
    ],
  },
  {
    ref: "TRF-0236",
    date: "20 Oct 2026",
    from: "Milimani",
    to: "Westlands Main",
    sentBy: "John Otieno",
    lines: [
      { item: "Metformin 500 mg, 100s", unit: "packs", sent: 10, unitCost: 330 },
      { item: "Glucometer strips, 50s", unit: "packs", sent: 6, unitCost: 1450 },
    ],
  },
];

function TransfersDemo() {
  const [ref, setRef] = React.useState("TRF-0234");
  const note = React.useRef<HTMLDivElement>(null);
  const t = TRANSFERS.find((x) => x.ref === ref) ?? TRANSFERS[0];
  return (
    <>
      <Example title="Transfer reports" description="Westlands Main. All transfers, what's on its way here, and what went missing between branches. Print opens the transfer note below">
        <TransferReports transfers={TRANSFERS} branch="Westlands Main" onPrint={setRef} />
      </Example>
      <Example title={`Transfer note ${t.ref}`} description="Travels with the goods; the receiving branch writes what arrived and signs. A received note shows the shortfall" className="space-y-3 bg-muted/40">
        <div className="flex justify-end">
          <PrintButton target={note} paper="a4" label={`Print ${t.ref}`} />
        </div>
        <div className="overflow-x-auto">
          <TransferNote ref={note} transfer={t} company="Medora Pharmacy Ltd" printedBy="Grace Wanjiru" printedAt="20 Oct 2026, 16:05" />
        </div>
      </Example>
    </>
  );
}

export function InventoryReportsSection() {
  return (
    <Section
      id="inventory-reports"
      title="Branch comparison, issues, transfers, supplier returns and purchase ageing"
      description="RP-04, IN-19, IN-23 and IN-25. Flags are words beside the figure, never colour alone, and every total is the sum of the rows above it."
    >
      <Example title="Branch comparison" description="Stock now, per branch; dead stock means no sales anywhere in the last 90 days" bare>
        <div className="p-4">
          <BranchComparison
            branches={BRANCHES}
            period="last 90 days"
            rows={[
              { item: "Panadol 500 mg", unit: "strips", reorder: 80, soldInPeriod: 2140, qty: { "Westlands Main": 420, Kilimani: 140, Nyali: 60, Milimani: 210 } },
              { item: "Amoxil 500 mg, 21s", unit: "packs", reorder: 20, soldInPeriod: 310, qty: { "Westlands Main": 48, Kilimani: 22, Nyali: 0, Milimani: 15 } },
              { item: "Metformin 500 mg, 100s", unit: "packs", reorder: 10, soldInPeriod: 96, qty: { "Westlands Main": 14, Kilimani: 9, Nyali: 12, Milimani: -2 } },
              { item: "Calamine lotion 100 ml", unit: "bottles", reorder: 6, soldInPeriod: 0, qty: { "Westlands Main": 22, Kilimani: 8, Nyali: 12, Milimani: 4 } },
              { item: "Vitamin C 1000 mg, 20s", unit: "tubes", reorder: 10, soldInPeriod: 0, qty: { "Westlands Main": 30, Kilimani: 0, Nyali: 0, Milimani: 18 } },
            ]}
          />
        </div>
      </Example>
      <Example title="Internal issue report" description="1–20 Oct 2026. Stock used inside the business: not sales, so cost only">
        <InternalIssueReport
          issues={[
            { no: "ISS-0071", date: "20 Oct", from: "Store room", to: "Consultation room", staff: "Dr. Achieng", reference: "Clinic day", items: [{ name: "Gloves, box of 100", qty: 2, unitCost: 650 }, { name: "Spirit 500 ml", qty: 1, unitCost: 280 }] },
            { no: "ISS-0069", date: "16 Oct", from: "Dispensary", to: "Staff use", staff: "Grace Wanjiru", items: [{ name: "Panadol 500 mg, strip", qty: 3, unitCost: 32 }] },
            { no: "ISS-0066", date: "9 Oct", from: "Store room", to: "Cleaning", staff: "Peter Kamau", reference: "Monthly", items: [{ name: "Hand sanitiser 5 L", qty: 1, unitCost: 2400 }] },
          ]}
        />
      </Example>
      <TransfersDemo />
      <div className="grid gap-4 xl:grid-cols-2">
        <Example title="Goods returned to suppliers" description="1–20 Oct 2026; the credit expected against what has been credited">
          <GoodsReturnsReport
            returns={[
              { ref: "SRN-0044", date: "18 Oct", supplier: "Harleys Ltd", items: "6 × Nivea Soft cream 200 ml (damaged)", value: 2730, credited: 0 },
              { ref: "SRN-0041", date: "11 Oct", supplier: "Dawa Ltd", items: "40 × Amoxil 500 mg (short expiry)", value: 8400, creditNote: "DCN-7781", credited: 4200 },
              { ref: "SRN-0039", date: "3 Oct", supplier: "Cosmos Ltd", items: "12 × Metformin 500 mg, 100s (recall)", value: 3960, creditNote: "CCN-1204", credited: 3960 },
            ]}
          />
        </Example>
        <Example title="Purchase ageing" description="All branches">
          <PurchaseAgeing
            asOf="20 Oct 2026"
            rows={[
              { supplier: "Harleys Ltd", buckets: [64200, 18300, 0, 0] },
              { supplier: "Dawa Ltd", buckets: [22500, 31800, 14100, 0] },
              { supplier: "Cosmos Ltd", buckets: [8800, 0, 0, 12600] },
              { supplier: "Surgipharm", buckets: [41000, 0, 0, 0] },
            ]}
          />
        </Example>
      </div>
    </Section>
  );
}
