"use client";

import { useEffect, useMemo, useState } from "react";

import { useLocale } from "@/components/providers/locale-provider";
import { composeGradeLabel } from "@/lib/grade-display";
import { formatQuantity } from "@/lib/formatters";
import type { ApiErrorResponse, DetailResponse } from "@/types/api";
import type { WarehouseLocationRecord, WarehouseRecord } from "@/types/master-data";
import type { PurchaseDetail, PurchaseListItem } from "@/types/purchase";
import type { ReceiptDetail, ReceiptListItem } from "@/types/receipt";

type ReceiptLineForm = {
  purchase_line_id: string;
  grade_id: string;
  grade_name: string;
  qty_ordered: string;
  qty_received: string;
  qty_accepted: string;
  qty_rejected: string;
  unit_id: string;
  unit_code: string;
  unit_cost: string;
  warehouse_id: string;
  warehouse_location_id: string;
  notes: string;
};

type ReceiptForm = {
  purchase_id: string;
  receipt_date: string;
  notes: string;
  lines: ReceiptLineForm[];
};

const emptyForm = (): ReceiptForm => ({
  purchase_id: "",
  receipt_date: new Date().toISOString().slice(0, 10),
  notes: "",
  lines: []
});

export function ReceiptsClient() {
  const { dict, locale } = useLocale();
  const [items, setItems] = useState<ReceiptListItem[]>([]);
  const [purchases, setPurchases] = useState<PurchaseListItem[]>([]);
  const [warehouses, setWarehouses] = useState<WarehouseRecord[]>([]);
  const [locations, setLocations] = useState<WarehouseLocationRecord[]>([]);
  const [form, setForm] = useState<ReceiptForm>(emptyForm);
  const [selectedReceipt, setSelectedReceipt] = useState<ReceiptDetail | null>(null);
  const [loading, setLoading] = useState(true);
  const [submitting, setSubmitting] = useState(false);
  const [error, setError] = useState<string | null>(null);

  async function loadAll() {
    setLoading(true);
    setError(null);
    try {
      const [receiptsRes, purchasesRes, warehousesRes, locationsRes] = await Promise.all([
        fetch("/api/v1/receipts", { cache: "no-store" }),
        fetch("/api/v1/purchases?purchase_type=REGULAR", { cache: "no-store" }),
        fetch("/api/v1/warehouses", { cache: "no-store" }),
        fetch("/api/v1/warehouse-locations", { cache: "no-store" })
      ]);

      const receiptsPayload = (await receiptsRes.json()) as { data: ReceiptListItem[] };
      const purchasesPayload = (await purchasesRes.json()) as { data: PurchaseListItem[] };
      const warehousesPayload = (await warehousesRes.json()) as { data: WarehouseRecord[] };
      const locationsPayload = (await locationsRes.json()) as { data: WarehouseLocationRecord[] };

      if (!receiptsRes.ok || !purchasesRes.ok || !warehousesRes.ok || !locationsRes.ok) {
        throw new Error(dict.receipts.loadingError);
      }

      setItems(receiptsPayload.data);
      setPurchases(purchasesPayload.data);
      setWarehouses(warehousesPayload.data);
      setLocations(locationsPayload.data);
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.receipts.loadingError);
    } finally {
      setLoading(false);
    }
  }

  useEffect(() => {
    void loadAll();
  }, []);

  async function handlePurchaseChange(purchaseId: string) {
    setForm((current) => ({ ...current, purchase_id: purchaseId }));
    if (!purchaseId) {
      setForm(emptyForm());
      return;
    }

    try {
      const response = await fetch(`/api/v1/purchases/${purchaseId}`, { cache: "no-store" });
      const payload = (await response.json()) as DetailResponse<PurchaseDetail> | ApiErrorResponse;
      if (!response.ok || !("data" in payload)) {
        throw new Error(dict.purchases.detailError);
      }
      const detail = payload.data;
      setForm((current) => ({
        ...current,
        purchase_id: detail.id,
        lines: detail.lines.map((line) => ({
          purchase_line_id: line.id,
          grade_id: line.grade?.id ?? "",
          grade_name: line.grade?.name ?? "-",
          qty_ordered: String(line.qty_ordered),
          qty_received: String(line.qty_ordered),
          qty_accepted: String(line.qty_ordered),
          qty_rejected: "0",
          unit_id: line.unit.id,
          unit_code: line.unit.code,
          unit_cost: String(line.unit_price),
          warehouse_id: "",
          warehouse_location_id: "",
          notes: line.notes ?? ""
        }))
      }));
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.purchases.detailError);
    }
  }

  function resetForm() {
    setForm(emptyForm());
    setSelectedReceipt(null);
    setError(null);
  }

  function updateLine(index: number, patch: Partial<ReceiptLineForm>) {
    setForm((current) => ({
      ...current,
      lines: current.lines.map((line, i) => {
        if (i !== index) return line;
        const updated = { ...line, ...patch };
        if ("qty_received" in patch && patch.qty_received !== undefined) {
          updated.qty_accepted = patch.qty_received;
          updated.qty_rejected = "0";
        }
        return updated;
      })
    }));
  }

  async function createReceipt() {
    setSubmitting(true);
    setError(null);
    try {
      const response = await fetch("/api/v1/receipts", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          purchase_id: form.purchase_id,
          receipt_date: form.receipt_date,
          notes: form.notes,
          lines: form.lines.map((line) => ({
            purchase_line_id: line.purchase_line_id,
            grade_id: line.grade_id || null,
            qty_received: Number(line.qty_received),
            qty_accepted: Number(line.qty_accepted),
            qty_rejected: Number(line.qty_rejected),
            unit_id: line.unit_id,
            unit_cost: Number(line.unit_cost),
            warehouse_id: line.warehouse_id,
            warehouse_location_id: line.warehouse_location_id || null,
            notes: line.notes
          }))
        })
      });
      const payload = (await response.json()) as DetailResponse<ReceiptDetail> | ApiErrorResponse;
      if (!response.ok) {
        if ("errors" in payload && payload.errors) {
          const firstError = Object.values(payload.errors)[0]?.[0];
          throw new Error(firstError ?? payload.message ?? (locale === "id" ? "Validasi gagal" : "Validation failed"));
        }
        throw new Error("message" in payload ? payload.message : dict.common.requestFailed);
      }
      if (!("data" in payload)) throw new Error(dict.receipts.createError);
      resetForm();
      await loadAll();
      await openReceipt(payload.data.id);
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.receipts.createError);
    } finally {
      setSubmitting(false);
    }
  }

  async function openReceipt(id: string) {
    setError(null);
    try {
      const response = await fetch(`/api/v1/receipts/${id}`, { cache: "no-store" });
      const payload = (await response.json()) as DetailResponse<ReceiptDetail> | ApiErrorResponse;
      if (!response.ok || !("data" in payload)) {
        throw new Error(dict.receipts.detailError);
      }
      setSelectedReceipt(payload.data);
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.receipts.detailError);
    }
  }

  async function generateLots(receiptId: string) {
    setError(null);
    try {
      const response = await fetch(`/api/v1/receipts/${receiptId}/generate-lots`, {
        method: "POST"
      });
      const payload = await response.json();
      if (!response.ok) {
        throw new Error(payload.message ?? dict.receipts.generateError);
      }
      await loadAll();
      await openReceipt(receiptId);
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.receipts.generateError);
    }
  }

  const submittedPurchases = useMemo(
    () => purchases.filter((purchase) => purchase.status === "SUBMITTED"),
    [purchases]
  );

  return (
    <section className="grid gap-6 xl:grid-cols-[1.05fr_0.95fr]">
      <div className="ops-card p-6">
        <div className="flex items-start justify-between gap-4">
          <div>
            <p className="ops-overline">{dict.receipts.overline}</p>
            <h2 className="mt-2 text-[28px] font-semibold tracking-tight text-ink">{dict.receipts.createTitle}</h2>
            <p className="mt-3 text-[13px] leading-6 text-slate-500">{dict.receipts.helper}</p>
          </div>
          <button type="button" onClick={resetForm} className="ops-btn-secondary">{dict.common.formReset}</button>
        </div>

        <div className="mt-6 space-y-5">
          <label className="block">
            <span className="ops-label">{dict.receipts.purchase}</span>
            <select value={form.purchase_id} onChange={(event) => void handlePurchaseChange(event.target.value)} className="ops-select">
              <option value="">{dict.receipts.choosePurchase}</option>
              {submittedPurchases.map((purchase) => (
                <option key={purchase.id} value={purchase.id}>
                  {purchase.purchase_no} · {purchase.agent?.name || (locale === "id" ? "Pembelian bebas" : "Direct purchase")}
                </option>
              ))}
            </select>
          </label>

          <div className="grid gap-4 md:grid-cols-2">
            <Field label={dict.receipts.receiptDate} type="date" value={form.receipt_date} onChange={(value) => setForm((current) => ({ ...current, receipt_date: value }))} />
          </div>

          <label className="block">
            <span className="ops-label">{dict.common.notes}</span>
            <textarea value={form.notes} onChange={(event) => setForm((current) => ({ ...current, notes: event.target.value }))} rows={3} className="ops-textarea" />
          </label>

          <div className="rounded-lg border border-line/70 bg-slate-50/70 p-4">
            <h3 className="text-lg font-semibold text-ink">{dict.receipts.receiptLines}</h3>
            <div className="mt-4 space-y-4">
              {form.lines.length === 0 ? (
                <div className="text-sm text-ink/60">{dict.receipts.choosePurchaseToLoad}</div>
              ) : (
                form.lines.map((line, index) => {
                  const locationOptions = locations.filter(
                    (location) => location.warehouse_id === line.warehouse_id
                  );

                  return (
                    <div key={line.purchase_line_id} className="rounded border border-line/70 bg-white p-4">
                      <div className="mb-4 text-sm text-slate-500">
                        <span className="font-semibold text-ink">{line.grade_name}</span> · Ordered {formatQuantity(Number(line.qty_ordered), "id", line.unit_code)}
                      </div>
                      <div className="grid gap-4 md:grid-cols-2">
                        <Field label={`${dict.receipts.qtyReceived} (${line.unit_code})`} value={line.qty_received} onChange={(value) => updateLine(index, { qty_received: value })} />
                        <Field label={`${dict.receipts.qtyAccepted} (${line.unit_code})`} value={line.qty_accepted} onChange={(value) => updateLine(index, { qty_accepted: value })} />
                        <Field label={`${dict.receipts.qtyRejected} (${line.unit_code})`} value={line.qty_rejected} onChange={(value) => updateLine(index, { qty_rejected: value })} />
                        <Field label={dict.receipts.unitCost} value={line.unit_cost} onChange={(value) => updateLine(index, { unit_cost: value })} />
                        <SelectField label={dict.common.warehouse} value={line.warehouse_id} onChange={(value) => updateLine(index, { warehouse_id: value, warehouse_location_id: "" })} options={warehouses.map((warehouse) => ({ value: warehouse.id, label: `${warehouse.code} - ${warehouse.name}` }))} placeholder={dict.receipts.chooseWarehouse} />
                        <SelectField label={dict.common.location} value={line.warehouse_location_id} onChange={(value) => updateLine(index, { warehouse_location_id: value })} options={locationOptions.map((location) => ({ value: location.id, label: `${location.code} - ${location.name}` }))} placeholder={dict.purchases.optional} />
                      </div>
                      <label className="mt-4 block">
                        <span className="ops-label">{dict.receipts.lineNotes}</span>
                        <textarea value={line.notes} onChange={(event) => updateLine(index, { notes: event.target.value })} rows={2} className="ops-textarea" />
                      </label>
                    </div>
                  );
                })
              )}
            </div>
          </div>

          {error ? <div className="rounded border border-ember/30 bg-ember/10 px-4 py-3 text-sm text-ember">{error}</div> : null}

          <button type="button" disabled={submitting || form.lines.length === 0} onClick={() => void createReceipt()} className="ops-btn-primary">
            {submitting ? dict.common.processing : dict.receipts.saveDraft}
          </button>
        </div>
      </div>

      <div className="space-y-6">
        <div className="ops-table-shell">
          <div className="ops-section-head">
            <div>
            <p className="ops-title">{dict.receipts.listTitle}</p>
            <p className="ops-copy">{dict.receipts.listCopy}</p>
          </div>
          <div className="ops-chip-muted">{items.length} receipt</div>
        </div>
        {loading ? <div className="px-6 py-8 text-sm text-ink/60">{dict.common.loading}</div> : items.length === 0 ? <div className="px-6 py-8 text-sm text-ink/60">{dict.receipts.noData}</div> : (
            <div className="overflow-x-auto">
              <table className="ops-table">
                <thead>
                  <tr>
                    <th>{locale === "id" ? "Penerimaan" : "Receipt"}</th>
                    <th>{locale === "id" ? "Pembelian" : "Purchase"}</th>
                    <th>{locale === "id" ? "Lot" : "Lots"}</th>
                    <th>{locale === "id" ? "Status" : "Status"}</th>
                    <th>{locale === "id" ? "Aksi" : "Actions"}</th>
                  </tr>
                </thead>
                <tbody>
                  {items.map((item) => (
                    <tr key={item.id}>
                      <td className="font-semibold text-ink">{item.receipt_no}</td>
                      <td className="text-slate-600">{item.purchase.purchase_no}</td>
                      <td className="text-slate-600">{item.lot_count}</td>
                      <td>
                        <span className={item.status === "FINALIZED" ? "ops-chip-active" : "ops-chip-muted"}>{item.status}</span>
                      </td>
                      <td>
                        <div className="flex gap-2">
                          <button type="button" onClick={() => void openReceipt(item.id)} className="ops-btn-secondary">{dict.common.detail}</button>
                          {item.status !== "FINALIZED" ? <button type="button" onClick={() => void generateLots(item.id)} className="ops-btn-primary">{locale === "id" ? "Buat lot" : "Generate lots"}</button> : null}
                        </div>
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
          )}
        </div>

        {selectedReceipt ? (
          <div className="ops-card p-6">
            <div className="flex items-center justify-between gap-4">
              <div>
                <p className="ops-overline">{locale === "id" ? "Detail penerimaan" : "Receipt details"}</p>
                <h3 className="mt-2 text-2xl font-semibold text-ink">{selectedReceipt.receipt_no}</h3>
                <p className="mt-2 text-sm text-slate-500">{selectedReceipt.purchase.purchase_no}</p>
              </div>
              {selectedReceipt.status !== "FINALIZED" ? <button type="button" onClick={() => void generateLots(selectedReceipt.id)} className="ops-btn-primary">{locale === "id" ? "Buat lot" : "Generate lots"}</button> : null}
            </div>
            <div className="mt-5 space-y-4">
              {selectedReceipt.lines.map((line) => (
                <div key={line.id} className="rounded border border-line/70 bg-slate-50 p-4 text-sm text-slate-600">
                  <div className="font-medium text-ink">{line.grade?.name ?? "-"}</div>
                  <div className="mt-2">{locale === "id" ? "Diterima" : "Received"} {formatQuantity(line.qty_received, locale, line.unit.code)} / {locale === "id" ? "Diterima valid" : "Accepted"} {formatQuantity(line.qty_accepted, locale, line.unit.code)} / {locale === "id" ? "Ditolak" : "Rejected"} {formatQuantity(line.qty_rejected, locale, line.unit.code)}</div>
                  <div className="mt-1">{locale === "id" ? "Gudang" : "Warehouse"} {line.warehouse.name} · {locale === "id" ? "Lokasi" : "Location"} {line.location?.name ?? "-"}</div>
                </div>
              ))}
            </div>
            <div className="mt-6">
              <p className="text-sm font-semibold text-ink">{locale === "id" ? "Lot hasil" : "Generated lots"}</p>
              {selectedReceipt.generated_lots.length === 0 ? <p className="mt-2 text-sm text-ink/60">{locale === "id" ? "Belum ada lot yang dihasilkan." : "No lots have been generated yet."}</p> : (
                <div className="mt-3 space-y-2">
                  {selectedReceipt.generated_lots.map((lot) => (
                    <div key={lot.id} className="rounded border border-line/70 bg-white px-4 py-3 text-sm text-slate-600">
                      <span className="font-medium text-ink">{lot.lot_code}</span> · {lot.status}
                    </div>
                  ))}
                </div>
              )}
            </div>
          </div>
        ) : null}
      </div>
    </section>
  );
}

function Field({ label, value, onChange, type = "text", disabled = false }: { label: string; value: string; onChange?: (value: string) => void; type?: string; disabled?: boolean }) {
  return (
    <label className="block">
      <span className="ops-label">{label}</span>
      <input type={type} disabled={disabled} value={value} onChange={(event) => onChange?.(event.target.value)} className="ops-input" />
    </label>
  );
}

function SelectField({ label, value, onChange, options, placeholder }: { label: string; value: string; onChange: (value: string) => void; options: Array<{ value: string; label: string }>; placeholder: string }) {
  return (
    <label className="block">
      <span className="ops-label">{label}</span>
      <select value={value} onChange={(event) => onChange(event.target.value)} className="ops-select">
        <option value="">{placeholder}</option>
        {options.map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}
      </select>
    </label>
  );
}
