"use client";

import { ArrowRight, Banknote, Check, Eye } from "lucide-react";
import { useTranslation } from "react-i18next";

import type { SettlementBillRow } from "@/api/admin-agent-settlement";
import { AdminNoResourceState } from "@/components/admin/admin-no-resource-state";
import { AdminLoadingState } from "@/components/admin/admin-loading-state";
import { AdminRowActionsMenu } from "@/components/admin/admin-row-actions-menu";
import { AdminStatusBadge } from "@/components/admin/admin-status-badge";
import { AdminTableMoney, adminMoneyCellClassName } from "@/components/admin/admin-table-money";
import { signedMoneyClass } from "@/lib/admin-signed-money";
import { cn } from "@/lib/utils";
import { formatSettlementPeriodSpan } from "@/lib/agent-settlement-period-range";
import {
  formatSignedSettlementMoney,
} from "@/modules/settlement/settlement-signed-money";
import { formatDashboardMoneyMinor } from "@/modules/dashboard/use-dashboard-analytics";
import { describeBillPaymentDirection } from "@/modules/settlement/settlement-bill-display";
import { settlementBillOperableByBoundAgent } from "@/modules/settlement/settlement-bill-operable";
import {
  formatPlatformPartyLabel,
  SettlementDashCell,
} from "@/modules/settlement/settlement-party-cells";
import {
  settlementBillStatusLabel,
  settlementBillTypeLabel,
} from "@/modules/settlement/settlement-status-label";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";

type BillTypeFilter = "all" | "player" | "agent";

type SettlementBillsTableProps = {
  rows: SettlementBillRow[];
  loading: boolean;
  currencyCode: string;
  billTypeFilter?: BillTypeFilter;
  emptyMessage?: string;
  canOperate?: boolean;
  boundAgentId?: number | null;
  onOpenDetail: (billId: number) => void;
  onConfirmBill?: (row: SettlementBillRow) => void;
  onPayBill?: (row: SettlementBillRow) => void;
};

function billRowTone(row: SettlementBillRow): string {
  if (row.bill_type === "player") {
    return "border-l-2 border-l-sky-300/80";
  }
  if (row.bill_type === "agent") {
    return "border-l-2 border-l-amber-300/80 bg-amber-50/20";
  }
  if (row.bill_type === "adjustment" || row.bill_type === "reversal") {
    return "border-l-2 border-l-emerald-300/80 bg-emerald-50/20";
  }
  if (row.bill_type === "bad_debt") {
    return "border-l-2 border-l-rose-300/80 bg-rose-50/20";
  }

  return "";
}

function billTypeTone(row: SettlementBillRow): string {
  if (row.bill_type === "player") {
    return "border-sky-200 bg-sky-50 text-sky-700";
  }
  if (row.bill_type === "agent") {
    return "border-amber-200 bg-amber-50 text-amber-800";
  }
  if (row.bill_type === "adjustment" || row.bill_type === "reversal") {
    return "border-emerald-200 bg-emerald-50 text-emerald-700";
  }
  if (row.bill_type === "bad_debt") {
    return "border-rose-200 bg-rose-50 text-rose-700";
  }

  return "border-border/70 bg-muted/25 text-muted-foreground";
}

function unpaidMoneyClass(row: SettlementBillRow): string {
  if (row.unpaid_amount <= 0) {
    return "text-muted-foreground";
  }
  if (row.status === "overdue") {
    return "font-medium text-destructive";
  }

  return "font-medium text-amber-800 dark:text-amber-300";
}

function paidMoneyClass(row: SettlementBillRow): string {
  if ((row.paid_amount ?? 0) <= 0) {
    return "text-muted-foreground";
  }
  if (row.unpaid_amount > 0) {
    return "font-medium text-amber-800 dark:text-amber-300";
  }

  return "font-medium text-emerald-700";
}

function ownerPartyLabel(row: SettlementBillRow): string | null {
  if (row.bill_type === "player") {
    return row.player_username ?? row.owner_label ?? null;
  }
  if (row.bill_type === "agent") {
    return row.owner_party_label ?? row.owner_label ?? null;
  }

  return row.owner_label ?? null;
}

export function SettlementBillsTable({
  rows,
  loading,
  currencyCode,
  billTypeFilter = "all",
  emptyMessage,
  canOperate = false,
  boundAgentId = null,
  onOpenDetail,
  onConfirmBill,
  onPayBill,
}: SettlementBillsTableProps): React.ReactElement {
  const { t } = useTranslation(["settlementCenter", "agents", "common"]);

  const agentView = billTypeFilter === "agent";
  const playerView = billTypeFilter === "player";
  const mixedView = billTypeFilter === "all";

  if (loading) {
    return <AdminLoadingState />;
  }

  if (rows.length === 0) {
    return <AdminNoResourceState message={emptyMessage} />;
  }

  return (
    <div className="admin-table-shell overflow-x-auto">
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>{t("columns.billId", { defaultValue: "账单 ID" })}</TableHead>
            <TableHead>{t("columns.period", { defaultValue: "账期" })}</TableHead>
            <TableHead>{t("columns.type", { defaultValue: "类型" })}</TableHead>
            {playerView ? (
              <>
                <TableHead>{t("columns.playerAccount", { defaultValue: "玩家账号" })}</TableHead>
                <TableHead>{t("columns.playerId", { defaultValue: "玩家 ID" })}</TableHead>
                <TableHead>{t("columns.directAgent", { defaultValue: "直属代理" })}</TableHead>
              </>
            ) : null}
            {agentView ? (
              <TableHead>{t("columns.owner", { defaultValue: "本方" })}</TableHead>
            ) : null}
            {mixedView ? (
              <TableHead>{t("columns.owner", { defaultValue: "本方" })}</TableHead>
            ) : null}
            <TableHead>{t("billDisplay.settlementFlow", { defaultValue: "谁付谁" })}</TableHead>
            <TableHead>{t("columns.superiorAgent", { defaultValue: "上级" })}</TableHead>
            {!playerView ? (
              <TableHead className="text-right">{t("columns.gross", { defaultValue: "输赢" })}</TableHead>
            ) : null}
            <TableHead className="text-right">{t("billDisplay.settlementAmount", { defaultValue: "结算金额" })}</TableHead>
            <TableHead className="text-right">{t("columns.paid", { defaultValue: "已收付" })}</TableHead>
            <TableHead className="text-right">{t("columns.unpaid", { defaultValue: "未结" })}</TableHead>
            <TableHead>{t("columns.status", { defaultValue: "状态" })}</TableHead>
            <TableHead className="sticky right-0 z-10 w-14 bg-muted text-center shadow-[-1px_0_0_rgba(203,213,225,0.7)]">
              {t("common:table.actions", { defaultValue: "操作" })}
            </TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {rows.map((row) => {
            const direction = describeBillPaymentDirection(row, t);
            const operable =
              canOperate &&
              settlementBillOperableByBoundAgent(row, boundAgentId ? { id: boundAgentId } : null);
            const canConfirm = operable && row.status === "pending_confirm";
            const canPay =
              operable &&
              ["confirmed", "partial_paid", "overdue"].includes(row.status) &&
              row.unpaid_amount > 0;

            const rowActions = [
              ...(canConfirm && onConfirmBill
                ? [
                    {
                      key: "confirm",
                      label: t("billsPanel.confirmOneBtn", { defaultValue: "确认" }),
                      icon: Check,
                      onClick: () => onConfirmBill(row),
                    },
                  ]
                : []),
              ...(canPay && onPayBill
                ? [
                    {
                      key: "pay",
                      label: t("billsPanel.payBtn", { defaultValue: "收付" }),
                      icon: Banknote,
                      onClick: () => onPayBill(row),
                    },
                  ]
                : []),
              {
                key: "detail",
                label: t("actions.detail", { defaultValue: "详情" }),
                icon: Eye,
                onClick: () => onOpenDetail(row.id),
              },
            ];

            return (
              <TableRow key={row.id} className={billRowTone(row)}>
                <TableCell className="font-mono text-xs">{row.id}</TableCell>
                <TableCell className="whitespace-nowrap text-xs text-muted-foreground">
                  {formatSettlementPeriodSpan(row.period_start, row.period_end)}
                </TableCell>
                <TableCell>
                  <span
                    className={cn(
                      "inline-flex rounded-full border px-2 py-0.5 text-xs font-medium",
                      billTypeTone(row),
                    )}
                  >
                    {settlementBillTypeLabel(row.bill_type, t)}
                  </span>
                </TableCell>
                {playerView ? (
                  <>
                    <TableCell>
                      <SettlementDashCell value={row.player_username ?? row.owner_label} />
                    </TableCell>
                    <TableCell className="font-mono text-xs">
                      <SettlementDashCell
                        value={row.player_site_player_id ?? row.player_id_display ?? row.owner_id}
                        mono
                      />
                    </TableCell>
                    <TableCell className="text-sm">
                      <SettlementDashCell value={row.direct_agent_label} />
                    </TableCell>
                  </>
                ) : null}
                {agentView ? (
                  <TableCell className="text-sm">
                    <SettlementDashCell value={ownerPartyLabel(row)} />
                  </TableCell>
                ) : null}
                {mixedView ? (
                  <TableCell className="text-sm">
                    <SettlementDashCell value={ownerPartyLabel(row)} />
                  </TableCell>
                ) : null}
                <TableCell className="min-w-[10rem] text-sm">
                  <div className="flex flex-wrap items-center gap-1 text-foreground">
                    <span className="font-medium">{direction.payer}</span>
                    <ArrowRight className="size-3.5 shrink-0 text-muted-foreground" aria-hidden />
                    <span className="font-medium">{direction.payee}</span>
                  </div>
                </TableCell>
                <TableCell className="text-sm">
                  {formatPlatformPartyLabel(row.superior_agent_label, t)}
                </TableCell>
                {!playerView ? (
                  <TableCell
                    className={cn(
                      "text-right tabular-nums",
                      row.gross_win_loss != null
                        ? signedMoneyClass(row.gross_win_loss)
                        : "text-muted-foreground",
                    )}
                  >
                    {row.gross_win_loss != null ? (
                      <div>{formatSignedSettlementMoney(row.gross_win_loss, currencyCode)}</div>
                    ) : (
                      "—"
                    )}
                  </TableCell>
                ) : null}
                <TableCell className={adminMoneyCellClassName(cn("text-right", signedMoneyClass(row.net_amount, true)))}>
                  <AdminTableMoney>
                    {formatDashboardMoneyMinor(direction.amount, currencyCode)}
                  </AdminTableMoney>
                </TableCell>
                <TableCell className={adminMoneyCellClassName(cn("text-right", paidMoneyClass(row)))}>
                  <AdminTableMoney>
                    {formatDashboardMoneyMinor(row.paid_amount ?? 0, currencyCode)}
                  </AdminTableMoney>
                </TableCell>
                <TableCell className={adminMoneyCellClassName(cn("text-right", unpaidMoneyClass(row)))}>
                  <AdminTableMoney>
                    {formatDashboardMoneyMinor(row.unpaid_amount, currencyCode)}
                  </AdminTableMoney>
                </TableCell>
                <TableCell>
                  <AdminStatusBadge status={row.status}>
                    {settlementBillStatusLabel(row.status, t)}
                  </AdminStatusBadge>
                </TableCell>
                <TableCell
                  className="sticky right-0 z-10 bg-card text-center shadow-[-1px_0_0_rgba(203,213,225,0.7)]"
                  onClick={(e) => e.stopPropagation()}
                >
                  <AdminRowActionsMenu actions={rowActions} />
                </TableCell>
              </TableRow>
            );
          })}
        </TableBody>
      </Table>
    </div>
  );
}
