import type { ChatInvoice } from "@/mocks/dshnChatThreads";
import { useWeb } from "../WebContext";
import { WBtn, StatusPill, cn } from "../WebUI";

export default function ChatInvoiceBubble({
  invoice,
  time,
  approved,
  showActions,
  onApprove,
  onContinue,
}: {
  invoice: ChatInvoice;
  time: string;
  approved: boolean;
  showActions: boolean;
  onApprove: () => void;
  onContinue: () => void;
}) {
  const { t } = useWeb();

  return (
    <div className="flex justify-center">
      <div className="flex max-w-full flex-col items-center gap-1">
        <div className="w-full max-w-[480px] overflow-hidden rounded-[20px] border border-accent-300 bg-background-100">
          <div className="flex items-center gap-2 border-b border-accent-100 bg-accent-50 px-4 py-3">
            <i className="ri-file-list-3-line text-[18px] leading-none text-accent-800" />
            <span className="text-[14px] font-bold text-foreground-950">{t("invoice")}</span>
            <span className="ms-auto whitespace-nowrap text-[10.5px] text-foreground-600">
              {invoice.date}
            </span>
          </div>

          <div className="grid grid-cols-[minmax(0,1fr)_48px_74px_86px] gap-1.5 border-b border-background-200 px-4 py-2">
            <span className="text-[10.5px] font-semibold text-foreground-500">{t("product")}</span>
            <span className="text-center text-[10.5px] font-semibold text-foreground-500">{t("quantity")}</span>
            <span className="text-center text-[10.5px] font-semibold text-foreground-500">{t("price")}</span>
            <span className="text-center text-[10.5px] font-semibold text-foreground-500">{t("total")}</span>
          </div>

          <div className="divide-y divide-background-200">
            {invoice.items.map((it) => (
              <div
                key={it.name}
                className="grid grid-cols-[minmax(0,1fr)_48px_74px_86px] items-start gap-1.5 px-4 py-2.5"
              >
                <span className="text-[12px] leading-snug text-foreground-900">{it.name}</span>
                <span className="text-center text-[12px] text-foreground-700">{it.qty}</span>
                <span className="whitespace-nowrap text-center text-[12px] text-foreground-700">
                  {it.price.toLocaleString("en-US")} ر.س
                </span>
                <span className="whitespace-nowrap text-center text-[12px] font-semibold text-foreground-950">
                  {(it.qty * it.price).toLocaleString("en-US")} ر.س
                </span>
              </div>
            ))}
          </div>

          <div className="flex items-center justify-between gap-2 bg-background-200/40 px-4 py-3">
            <span className="text-[13px] font-bold text-foreground-900">{t("total")}</span>
            <span className="whitespace-nowrap text-[16px] font-bold text-foreground-950">
              {invoice.total.toLocaleString("en-US")} ر.س
            </span>
          </div>

          <p className="px-4 pt-3 text-[11px] leading-relaxed text-foreground-500">{t("invoiceNote")}</p>

          {approved ? (
            <div className="px-4 pb-4 pt-3">
              <StatusPill tone="success" icon="ri-checkbox-circle-line">
                {t("approvedInvoice")}
              </StatusPill>
            </div>
          ) : showActions ? (
            <div className="flex flex-col gap-2.5 p-3.5">
              <WBtn full icon="ri-check-line" onClick={onApprove}>
                {t("invoiceAgree")}
              </WBtn>
              <WBtn full variant="secondary" onClick={onContinue}>
                {t("invoiceContinue")}
              </WBtn>
            </div>
          ) : (
            <div className="h-3" />
          )}
        </div>
        <span className={cn("px-1 text-[10.5px] text-foreground-400")}>{time}</span>
      </div>
    </div>
  );
}