"use client";

import { Printer, Tags } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import JsBarcode from "jsbarcode";
import QRCode from "qrcode";

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);
  }

  async function fetchReceiptDetail(id: string) {
    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("message" in payload ? payload.message : dict.receipts.detailError);
    }
    return payload.data;
  }

  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 {
      setSelectedReceipt(await fetchReceiptDetail(id));
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.receipts.detailError);
    }
  }

  async function printReceiptById(id: string) {
    setError(null);
    try {
      printReceipt(await fetchReceiptDetail(id));
    } catch (err) {
      setError(err instanceof Error ? err.message : dict.receipts.detailError);
    }
  }

  async function printLotLabelsById(id: string) {
    setError(null);
    try {
      printLotLabels(await fetchReceiptDetail(id));
    } 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);
    }
  }

  function printReceipt(detail: ReceiptDetail) {
    setError(null);
    const printWindow = window.open("", "_blank", "width=1120,height=760");
    if (!printWindow) {
      window.alert(locale === "id" ? "Popup cetak diblokir browser. Izinkan pop-up untuk situs ini lalu coba lagi." : "Print popup was blocked by the browser. Allow pop-ups for this site and try again.");
      return;
    }

    try {
      printWindow.document.open();
      printWindow.document.write(buildReceiptPrintHtml(detail, locale));
      printWindow.document.close();
    } catch (err) {
      if (!printWindow.closed) {
        printWindow.close();
      }
      setError(err instanceof Error ? err.message : locale === "id" ? "Gagal menyiapkan cetak penerimaan." : "Failed to prepare receipt print.");
    }
  }

  async function printLotLabels(detail: ReceiptDetail) {
    setError(null);
    if (detail.generated_lots.length === 0) {
      setError(locale === "id" ? "Belum ada lot yang bisa dicetak. Buat lot terlebih dahulu." : "No lot labels to print yet. Generate lots first.");
      return;
    }

    const printWindow = window.open("", "_blank", "width=900,height=760");
    if (!printWindow) {
      window.alert(locale === "id" ? "Popup cetak diblokir browser. Izinkan pop-up untuk situs ini lalu coba lagi." : "Print popup was blocked by the browser. Allow pop-ups for this site and try again.");
      return;
    }

    try {
      printWindow.document.open();
      printWindow.document.write(`<!doctype html><html><head><title>Menyiapkan Label</title></head><body style="font-family: Arial, sans-serif; padding: 24px;">${locale === "id" ? "Menyiapkan label lot..." : "Preparing lot labels..."}</body></html>`);
      printWindow.document.close();

      const printableLots = await Promise.all(
        detail.generated_lots.map(async (lot) => {
          const qrValue = lot.qr_code_value || lot.barcode_value || lot.lot_code;
          const barcodeValue = lot.barcode_value || lot.qr_code_value || lot.lot_code;
          return {
            lot,
            qrValue,
            barcodeValue,
            qrDataUrl: await QRCode.toDataURL(qrValue, {
              width: 160,
              margin: 1,
              errorCorrectionLevel: "M"
            }),
            barcodeSvg: buildBarcodeSvg(barcodeValue)
          };
        })
      );

      printWindow.document.open();
      printWindow.document.write(buildLotLabelsPrintHtml(detail, locale, printableLots));
      printWindow.document.close();
    } catch (err) {
      if (!printWindow.closed) {
        printWindow.close();
      }
      setError(err instanceof Error ? err.message : locale === "id" ? "Gagal menyiapkan label lot." : "Failed to prepare lot labels.");
    }
  }

  const receiptedPurchaseIds = useMemo(
    () => new Set(items.map((item) => item.purchase.id)),
    [items]
  );

  const receivablePurchases = useMemo(
    () => purchases.filter((purchase) => purchase.status !== "CANCELLED" && !receiptedPurchaseIds.has(purchase.id)),
    [purchases, receiptedPurchaseIds]
  );

  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>
              {receivablePurchases.map((purchase) => (
                <option key={purchase.id} value={purchase.id}>
                  {purchase.purchase_no} · {purchase.agent?.name || dict.receipts.directPurchase}
                </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> · {dict.receipts.ordered} {formatQuantity(Number(line.qty_ordered), locale, 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} {dict.receipts.countSuffix}</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>{dict.receipts.receiptNo}</th>
                    <th>{dict.receipts.purchase}</th>
                    <th>{dict.receipts.lots}</th>
                    <th>{dict.receipts.status}</th>
                    <th>{dict.receipts.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 flex-nowrap gap-2">
                          <button type="button" onClick={() => void openReceipt(item.id)} className="ops-btn-secondary whitespace-nowrap">{dict.common.detail}</button>
                          {item.status !== "FINALIZED" ? <button type="button" onClick={() => void generateLots(item.id)} className="ops-btn-primary whitespace-nowrap">{dict.receipts.generateLots}</button> : null}
                          <button
                            type="button"
                            onClick={() => void printReceiptById(item.id)}
                            className="ops-btn-secondary whitespace-nowrap"
                          >
                            <Printer className="h-4 w-4" />
                            {locale === "id" ? "Receipt" : "Receipt"}
                          </button>
                          {item.lot_count > 0 ? (
                            <button
                              type="button"
                              onClick={() => void printLotLabelsById(item.id)}
                              className="ops-btn-secondary whitespace-nowrap"
                            >
                              <Tags className="h-4 w-4" />
                              {locale === "id" ? "Label lot" : "Lot labels"}
                            </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">{dict.receipts.detailTitle}</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>
              <div className="flex flex-wrap items-center gap-2">
                <button type="button" onClick={() => printReceipt(selectedReceipt)} className="ops-btn-secondary">
                  <Printer className="h-4 w-4" />
                  {locale === "id" ? "Cetak receipt" : "Print receipt"}
                </button>
                {selectedReceipt.generated_lots.length > 0 ? (
                  <button type="button" onClick={() => void printLotLabels(selectedReceipt)} className="ops-btn-secondary">
                    <Tags className="h-4 w-4" />
                    {locale === "id" ? "Cetak label lot" : "Print lot labels"}
                  </button>
                ) : null}
                {selectedReceipt.status !== "FINALIZED" ? <button type="button" onClick={() => void generateLots(selectedReceipt.id)} className="ops-btn-primary">{dict.receipts.generateLots}</button> : null}
              </div>
            </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">{dict.receipts.receivedShort} {formatQuantity(line.qty_received, locale, line.unit.code)} / {dict.receipts.acceptedShort} {formatQuantity(line.qty_accepted, locale, line.unit.code)} / {dict.receipts.rejectedShort} {formatQuantity(line.qty_rejected, locale, line.unit.code)}</div>
                  <div className="mt-1">{dict.common.warehouse} {line.warehouse.name} · {dict.common.location} {line.location?.name ?? "-"}</div>
                </div>
              ))}
            </div>
            <div className="mt-6">
              <p className="text-sm font-semibold text-ink">{dict.receipts.generatedLots}</p>
              {selectedReceipt.generated_lots.length === 0 ? <p className="mt-2 text-sm text-ink/60">{dict.receipts.noGeneratedLots}</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.grade?.name ?? "-"} · {formatQuantity(lot.original_qty, locale, lot.unit.code)} · {lot.status}
                    </div>
                  ))}
                </div>
              )}
            </div>
          </div>
        ) : null}
      </div>
    </section>
  );
}

function escapeHtml(value: string) {
  return value
    .replaceAll("&", "&amp;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll('"', "&quot;")
    .replaceAll("'", "&#39;");
}

function buildBarcodeSvg(value: string) {
  const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  JsBarcode(svg, value, {
    format: "CODE128",
    displayValue: false,
    margin: 0,
    width: 1.5,
    height: 44
  });

  svg.setAttribute("width", "100%");
  svg.setAttribute("height", "52");
  svg.setAttribute("preserveAspectRatio", "none");

  return svg.outerHTML;
}

function buildReceiptPrintHtml(detail: ReceiptDetail, locale: string) {
  const isIndonesian = locale === "id";
  const safeReceiptNo = escapeHtml(detail.receipt_no);
  const safePurchaseNo = escapeHtml(detail.purchase.purchase_no);
  const safeDate = escapeHtml(new Date(detail.receipt_date).toLocaleDateString(isIndonesian ? "id-ID" : "en-US"));
  const safeNotes = escapeHtml(detail.notes ?? "-");
  const totalAccepted = detail.lines.reduce((sum, line) => sum + line.qty_accepted, 0);

  const rows = detail.lines.map((line, index) => {
    const safeGrade = escapeHtml(line.grade?.name ?? "-");
    const safeWarehouse = escapeHtml(line.warehouse.name);
    const safeLocation = escapeHtml(line.location?.name ?? "-");
    const safeLineNotes = escapeHtml(line.notes ?? "-");

    return `<tr>
      <td>${index + 1}</td>
      <td>${safeGrade}</td>
      <td>${escapeHtml(formatQuantity(line.qty_received, locale, line.unit.code))}</td>
      <td>${escapeHtml(formatQuantity(line.qty_accepted, locale, line.unit.code))}</td>
      <td>${escapeHtml(formatQuantity(line.qty_rejected, locale, line.unit.code))}</td>
      <td>${safeWarehouse}</td>
      <td>${safeLocation}</td>
      <td>${safeLineNotes}</td>
    </tr>`;
  }).join("");

  const lotRows = detail.generated_lots.map((lot, index) => {
    const safeGrade = escapeHtml(lot.grade?.name ?? "-");
    return `<tr>
      <td>${index + 1}</td>
      <td>${escapeHtml(lot.lot_code)}</td>
      <td>${safeGrade}</td>
      <td>${escapeHtml(formatQuantity(lot.original_qty, locale, lot.unit.code))}</td>
      <td>${escapeHtml(lot.status)}</td>
    </tr>`;
  }).join("");

  return `<!doctype html>
<html lang="${isIndonesian ? "id" : "en"}">
  <head>
    <meta charset="utf-8" />
    <title>${isIndonesian ? "Receipt Penerimaan" : "Receipt"} ${safeReceiptNo}</title>
    <style>
      body { margin: 0; background: #eef3f5; color: #182126; font-family: Arial, Helvetica, sans-serif; }
      .sheet { max-width: 1120px; margin: 0 auto; padding: 24px; }
      .paper { background: #fff; border: 1px solid #d9e2e7; padding: 28px; }
      .top { display: flex; justify-content: space-between; gap: 24px; border-bottom: 2px solid #182126; padding-bottom: 16px; }
      h1 { margin: 0; font-size: 28px; letter-spacing: 0; }
      .muted { color: #64748b; font-size: 12px; line-height: 1.5; }
      .meta { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 20px 0; }
      .box { border: 1px solid #d9e2e7; padding: 10px 12px; }
      .label { color: #64748b; font-size: 10px; font-weight: 700; text-transform: uppercase; }
      .value { margin-top: 4px; font-size: 14px; font-weight: 700; }
      table { width: 100%; border-collapse: collapse; margin-top: 14px; font-size: 12px; }
      th, td { border: 1px solid #d9e2e7; padding: 8px; text-align: left; vertical-align: top; }
      th { background: #f1f5f9; font-size: 10px; text-transform: uppercase; color: #475569; }
      h2 { margin: 24px 0 0; font-size: 16px; }
      @media print {
        body { background: #fff; }
        .sheet { padding: 0; }
        .paper { border: 0; }
      }
    </style>
  </head>
  <body onload="window.print()">
    <div class="sheet">
      <div class="paper">
        <div class="top">
          <div>
            <h1>${isIndonesian ? "Receipt Penerimaan" : "Goods Receipt"}</h1>
            <div class="muted">${safeReceiptNo}</div>
          </div>
          <div class="muted" style="text-align:right">
            ${isIndonesian ? "Pembelian" : "Purchase"}<br />
            <strong>${safePurchaseNo}</strong>
          </div>
        </div>
        <div class="meta">
          <div class="box"><div class="label">${isIndonesian ? "Tanggal" : "Date"}</div><div class="value">${safeDate}</div></div>
          <div class="box"><div class="label">${isIndonesian ? "Status" : "Status"}</div><div class="value">${escapeHtml(detail.status)}</div></div>
          <div class="box"><div class="label">${isIndonesian ? "Baris" : "Lines"}</div><div class="value">${detail.lines.length}</div></div>
          <div class="box"><div class="label">${isIndonesian ? "Total valid" : "Accepted total"}</div><div class="value">${escapeHtml(formatQuantity(totalAccepted, locale))}</div></div>
        </div>
        <div class="muted"><strong>${isIndonesian ? "Catatan" : "Notes"}:</strong> ${safeNotes}</div>
        <h2>${isIndonesian ? "Baris Penerimaan" : "Receipt Lines"}</h2>
        <table>
          <thead><tr><th>No</th><th>Grade</th><th>${isIndonesian ? "Diterima" : "Received"}</th><th>${isIndonesian ? "Valid" : "Accepted"}</th><th>${isIndonesian ? "Ditolak" : "Rejected"}</th><th>${isIndonesian ? "Gudang" : "Warehouse"}</th><th>${isIndonesian ? "Lokasi" : "Location"}</th><th>${isIndonesian ? "Catatan" : "Notes"}</th></tr></thead>
          <tbody>${rows}</tbody>
        </table>
        <h2>${isIndonesian ? "Lot Hasil" : "Generated Lots"}</h2>
        <table>
          <thead><tr><th>No</th><th>Lot</th><th>Grade</th><th>Qty</th><th>Status</th></tr></thead>
          <tbody>${lotRows || `<tr><td colspan="5">${isIndonesian ? "Belum ada lot." : "No lots yet."}</td></tr>`}</tbody>
        </table>
      </div>
    </div>
  </body>
</html>`;
}

function buildLotLabelsPrintHtml(
  detail: ReceiptDetail,
  locale: string,
  printableLots: Array<{
    lot: ReceiptDetail["generated_lots"][number];
    qrValue: string;
    barcodeValue: string;
    qrDataUrl: string;
    barcodeSvg: string;
  }>
) {
  const isIndonesian = locale === "id";
  const labels = printableLots.map(({ lot, qrValue, barcodeValue, qrDataUrl, barcodeSvg }) => {
    const safeLotCode = escapeHtml(lot.lot_code);
    const safeGrade = escapeHtml(lot.grade?.name ?? "-");
    const safeWarehouse = escapeHtml(lot.warehouse.name);
    const safeLocation = escapeHtml(lot.location?.name ?? "-");
    const safeQrValue = escapeHtml(qrValue);
    const safeBarcodeValue = escapeHtml(barcodeValue);

    return `<section class="label">
      <div class="label-head">
        <div>
          <div class="eyebrow">ABELBIRDNEST LOT</div>
          <h2>${safeLotCode}</h2>
        </div>
        <div class="status">${escapeHtml(lot.status)}</div>
      </div>
      <div class="body-grid">
        <div class="qr-box">
          <img src="${qrDataUrl}" alt="QR ${safeLotCode}" />
          <p class="scan-value">${safeQrValue}</p>
        </div>
        <div class="grid">
          <div><span>Grade</span><strong>${safeGrade}</strong></div>
          <div><span>Qty</span><strong>${escapeHtml(formatQuantity(lot.original_qty, locale, lot.unit.code))}</strong></div>
          <div><span>${isIndonesian ? "Gudang" : "Warehouse"}</span><strong>${safeWarehouse}</strong></div>
          <div><span>${isIndonesian ? "Lokasi" : "Location"}</span><strong>${safeLocation}</strong></div>
        </div>
      </div>
      <div class="barcode">
        <div class="barcode-svg">${barcodeSvg}</div>
        <p class="scan-value">${safeBarcodeValue}</p>
      </div>
      <div class="foot">
        <span>${isIndonesian ? "Receipt" : "Receipt"} ${escapeHtml(detail.receipt_no)}</span>
        <span>${escapeHtml(detail.receipt_date)}</span>
      </div>
    </section>`;
  }).join("");

  return `<!doctype html>
<html lang="${isIndonesian ? "id" : "en"}">
  <head>
    <meta charset="utf-8" />
    <title>${isIndonesian ? "Label Lot" : "Lot Labels"} ${escapeHtml(detail.receipt_no)}</title>
    <style>
      body { margin: 0; background: #f1f5f9; color: #111827; font-family: Arial, Helvetica, sans-serif; }
      .sheet { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; padding: 16px; }
      .label { break-inside: avoid; background: #fff; border: 1.5px solid #111827; border-radius: 6px; padding: 10px; min-height: 50mm; display: flex; flex-direction: column; gap: 8px; }
      .label-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
      .eyebrow { font-size: 9px; font-weight: 800; color: #64748b; letter-spacing: 0; }
      h2 { margin: 3px 0 0; font-size: 18px; letter-spacing: 0; word-break: break-word; }
      .status { border: 1px solid #111827; padding: 4px 7px; font-size: 10px; font-weight: 800; }
      .body-grid { display: grid; grid-template-columns: 34mm 1fr; gap: 8px; }
      .qr-box { border: 1px solid #cbd5e1; border-radius: 6px; padding: 5px; display: flex; flex-direction: column; align-items: center; justify-content: center; }
      .qr-box img { width: 28mm; height: 28mm; }
      .barcode { border: 1px solid #cbd5e1; border-radius: 6px; padding: 5px 7px; }
      .barcode-svg { height: 52px; }
      .scan-value { margin: 3px 0 0; text-align: center; font-size: 9px; font-weight: 800; letter-spacing: 0; word-break: break-all; }
      .grid { display: grid; grid-template-columns: 1fr; gap: 6px; }
      .grid div { border-top: 1px solid #e2e8f0; padding-top: 6px; }
      .grid span { display: block; font-size: 9px; color: #64748b; font-weight: 800; text-transform: uppercase; }
      .grid strong { display: block; margin-top: 2px; font-size: 12px; }
      .foot { margin-top: auto; display: flex; justify-content: space-between; gap: 10px; color: #64748b; font-size: 10px; }
      @media print {
        body { background: #fff; }
        .sheet { padding: 0; gap: 8mm; }
        .label { border-radius: 0; min-height: 50mm; }
      }
    </style>
  </head>
  <body onload="window.print()">
    <main class="sheet">${labels}</main>
  </body>
</html>`;
}

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>
  );
}
