"use client";

import { ArrowDownRight, ArrowUpRight, DollarSign } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Card } from "@/components/ui/card";
import { currency, formatDate } from "@/lib/utils";
import { FinancialReportResponse } from "@/types/reports";

interface FinancialReportTableProps {
  data: FinancialReportResponse;
  isLoading?: boolean;
}

export function FinancialReportTable({ data, isLoading = false }: FinancialReportTableProps) {
  return (
    <Card className="overflow-hidden shadow-xs border">
      <div className="flex flex-wrap items-center justify-between border-b bg-card p-4">
        <div>
          <h2 className="text-base font-bold text-foreground flex items-center gap-2">
            <DollarSign className="h-5 w-5 text-emerald-600 dark:text-emerald-400" />
            Relatório de Faturamento e Fluxo Financeiro
          </h2>
          <p className="text-xs text-muted-foreground">
            Detalhamento de entradas (contas a receber) e saídas (contas a pagar).
          </p>
        </div>

        <div className="flex items-center gap-4 text-xs">
          <div className="rounded-lg bg-emerald-500/10 p-2.5 border border-emerald-500/20">
            <span className="text-muted-foreground block">Total Entradas:</span>
            <span className="text-sm font-bold text-emerald-600 dark:text-emerald-400">
              {currency(data.summary.totalReceivables)}
            </span>
          </div>

          <div className="rounded-lg bg-rose-500/10 p-2.5 border border-rose-500/20">
            <span className="text-muted-foreground block">Total Saídas:</span>
            <span className="text-sm font-bold text-rose-600 dark:text-rose-400">
              {currency(data.summary.totalPayables)}
            </span>
          </div>

          <div className="rounded-lg bg-purple-500/10 p-2.5 border border-purple-500/20">
            <span className="text-muted-foreground block">Saldo do Período:</span>
            <span className="text-sm font-bold text-purple-600 dark:text-purple-400">
              {currency(data.summary.balance)}
            </span>
          </div>
        </div>
      </div>

      <div className="overflow-x-auto">
        <table className="w-full min-w-[750px] text-left text-sm">
          <thead className="bg-muted/50 text-xs uppercase text-muted-foreground">
            <tr>
              <th className="px-4 py-3.5 font-medium">Tipo</th>
              <th className="px-4 py-3.5 font-medium">Descrição</th>
              <th className="px-4 py-3.5 font-medium">Cliente / Fornecedor</th>
              <th className="px-4 py-3.5 font-medium">Vencimento</th>
              <th className="px-4 py-3.5 font-medium text-right">Valor (R$)</th>
              <th className="px-4 py-3.5 text-center font-medium">Status</th>
            </tr>
          </thead>
          <tbody className="divide-y">
            {isLoading ? (
              <tr>
                <td colSpan={6} className="p-8 text-center text-muted-foreground">
                  Carregando relatório financeiro...
                </td>
              </tr>
            ) : data.items.length === 0 ? (
              <tr>
                <td colSpan={6} className="p-8 text-center text-muted-foreground">
                  Nenhum lançamento financeiro encontrado no período selecionado.
                </td>
              </tr>
            ) : (
              data.items.map((item) => (
                <tr key={item.id} className="hover:bg-muted/40 transition-colors">
                  <td className="px-4 py-3.5">
                    {item.type === "RECEIVABLE" ? (
                      <span className="inline-flex items-center text-xs font-semibold text-emerald-600 dark:text-emerald-400 gap-1">
                        <ArrowUpRight className="h-4 w-4" /> Entrada
                      </span>
                    ) : (
                      <span className="inline-flex items-center text-xs font-semibold text-rose-600 dark:text-rose-400 gap-1">
                        <ArrowDownRight className="h-4 w-4" /> Saída
                      </span>
                    )}
                  </td>
                  <td className="px-4 py-3.5 font-medium text-foreground">
                    {item.description}
                  </td>
                  <td className="px-4 py-3.5 text-xs text-muted-foreground">
                    {item.entityName}
                  </td>
                  <td className="px-4 py-3.5 font-mono text-xs">
                    {formatDate(item.dueDate)}
                  </td>
                  <td
                    className={`px-4 py-3.5 text-right font-bold ${
                      item.type === "RECEIVABLE" ? "text-emerald-600" : "text-rose-600"
                    }`}
                  >
                    {item.type === "RECEIVABLE" ? "+" : "-"} {currency(item.amount)}
                  </td>
                  <td className="px-4 py-3.5 text-center">
                    <Badge tone={item.status === "PAID" ? "green" : "blue"}>
                      {item.status === "PAID" ? "PAGO / RECEBIDO" : "EM ABERTO"}
                    </Badge>
                  </td>
                </tr>
              ))
            )}
          </tbody>
        </table>
      </div>
    </Card>
  );
}
