import { memo } from "react";
import { PAYMENT_METHOD_SHORT } from "@/lib/constants";
import { formatDayMonth, formatShort } from "@/lib/format";
import { cn } from "@/lib/utils";
import type { CustomerStatus, GridCell as GridCellData } from "@/types";

/**
 * Warna sel: hijau = lunas, kuning = sebagian, merah = nunggak (lewat jatuh tempo),
 * putih = belum jatuh tempo, abu-abu = gratis/batal/berhenti/belum pasang/tidak ada tagihan.
 * Isi teks selalu ikut menjelaskan status, jadi tidak bergantung pada warna saja.
 */
export function cellAppearance(cell: GridCellData | null, customerStatus: CustomerStatus): { className: string; text: string; title: string } {
  if (!cell) {
    const label = customerStatus === "berhenti" ? "OFF" : customerStatus === "pending_pasang" ? "–" : "";
    return { className: "bg-slate-100 text-slate-400", text: label, title: "Tidak ada tagihan" };
  }
  switch (cell.status) {
    case "lunas":
      return {
        className: "bg-emerald-100 text-emerald-900 hover:bg-emerald-200",
        text: `${formatDayMonth(cell.paid_at)}${cell.method && cell.method !== "tunai" ? ` ${PAYMENT_METHOD_SHORT[cell.method]}` : ""}`,
        title: "Lunas",
      };
    case "sebagian":
      return {
        className: cn("bg-amber-100 text-amber-900 hover:bg-amber-200", cell.is_overdue && "ring-2 ring-inset ring-red-400"),
        text: `Kurang ${formatShort(cell.remaining)}`,
        title: cell.is_overdue ? "Sebagian · lewat jatuh tempo" : "Sebagian",
      };
    case "belum_bayar":
      return cell.is_overdue
        ? { className: "bg-red-100 font-semibold text-red-800 hover:bg-red-200", text: "Nunggak", title: "Belum bayar · lewat jatuh tempo" }
        : { className: "bg-white text-slate-400 hover:bg-slate-50", text: formatDayMonth(cell.due_date), title: "Belum jatuh tempo" };
    case "gratis":
      return { className: "bg-slate-100 text-sky-700", text: "FREE", title: "Gratis" };
    case "batal":
      return { className: "bg-slate-100 text-slate-400 line-through", text: "Batal", title: "Dibatalkan" };
  }
}

export const GridCellButton = memo(function GridCellButton({
  cell, customerStatus, onClick,
}: { cell: GridCellData | null; customerStatus: CustomerStatus; onClick: () => void }) {
  const look = cellAppearance(cell, customerStatus);
  return (
    <button
      type="button"
      onClick={onClick}
      title={look.title}
      className={cn("h-11 w-full whitespace-nowrap px-1 text-center text-[11px] leading-tight tabular-nums transition-colors", look.className)}
    >
      {look.text}
    </button>
  );
});

export function GridLegend() {
  const items = [
    ["bg-emerald-100 border-emerald-300", "Lunas (tgl bayar)"],
    ["bg-amber-100 border-amber-300", "Sebagian"],
    ["bg-red-100 border-red-300", "Nunggak"],
    ["bg-white border-slate-300", "Belum jatuh tempo"],
    ["bg-slate-100 border-slate-300", "Gratis / tidak ada tagihan"],
  ] as const;
  return (
    <div className="flex flex-wrap gap-x-3 gap-y-1 text-[11px] text-muted-foreground sm:gap-x-4 sm:text-xs">
      {items.map(([cls, label]) => (
        <span key={label} className="inline-flex items-center gap-1.5">
          <span className={cn("h-3 w-3 rounded-sm border", cls)} />
          {label}
        </span>
      ))}
    </div>
  );
}
