"use client";

import { Package } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Card } from "@/components/ui/card";
import { currency } from "@/lib/utils";
import { FleetReportResponse } from "@/types/reports";

interface FleetReportTableProps {
  data: FleetReportResponse;
  isLoading?: boolean;
}

export function FleetReportTable({ data, isLoading = false }: FleetReportTableProps) {
  return (
    <Card className="overflow-hidden shadow-xs border">
      <div className="border-b bg-card p-4">
        <h2 className="text-base font-bold text-foreground flex items-center gap-2">
          <Package className="h-5 w-5 text-blue-600 dark:text-blue-400" />
          Relatório de Ocupação e Rentabilidade da Frota
        </h2>
        <p className="text-xs text-muted-foreground">
          Análise de faturamento acumulado e custos operacionais por equipamento.
        </p>
      </div>

      <div className="overflow-x-auto">
        <table className="w-full min-w-[800px] 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">Código</th>
              <th className="px-4 py-3.5 font-medium">Equipamento</th>
              <th className="px-4 py-3.5 font-medium">Categoria</th>
              <th className="px-4 py-3.5 font-medium">Status Atual</th>
              <th className="px-4 py-3.5 font-medium text-center">Locações</th>
              <th className="px-4 py-3.5 font-medium text-right">Receita Gerada</th>
              <th className="px-4 py-3.5 font-medium text-right">Custo Manutenção</th>
              <th className="px-4 py-3.5 font-medium text-right">Saldo Líquido</th>
            </tr>
          </thead>
          <tbody className="divide-y">
            {isLoading ? (
              <tr>
                <td colSpan={8} className="p-8 text-center text-muted-foreground">
                  Carregando relatório da frota...
                </td>
              </tr>
            ) : data.items.length === 0 ? (
              <tr>
                <td colSpan={8} className="p-8 text-center text-muted-foreground">
                  Nenhum equipamento cadastrado na frota.
                </td>
              </tr>
            ) : (
              data.items.map((item) => (
                <tr key={item.id} className="hover:bg-muted/40 transition-colors">
                  <td className="px-4 py-3.5 font-mono text-xs font-bold text-foreground">
                    {item.code}
                  </td>
                  <td className="px-4 py-3.5 font-semibold text-foreground">
                    {item.name}
                  </td>
                  <td className="px-4 py-3.5 text-xs text-muted-foreground">
                    {item.categoryName}
                  </td>
                  <td className="px-4 py-3.5">
                    <Badge
                      tone={
                        item.status === "AVAILABLE"
                          ? "green"
                          : item.status === "RENTED"
                          ? "blue"
                          : item.status === "MAINTENANCE"
                          ? "yellow"
                          : "neutral"
                      }
                    >
                      {item.status === "AVAILABLE"
                        ? "DISPONÍVEL"
                        : item.status === "RENTED"
                        ? "ALUGADO"
                        : item.status === "MAINTENANCE"
                        ? "OFICINA"
                        : item.status}
                    </Badge>
                  </td>
                  <td className="px-4 py-3.5 text-center font-bold">
                    {item.totalRentals}
                  </td>
                  <td className="px-4 py-3.5 text-right font-semibold text-emerald-600">
                    {currency(item.totalRevenue)}
                  </td>
                  <td className="px-4 py-3.5 text-right font-semibold text-rose-600">
                    {currency(item.totalMaintenanceCost)}
                  </td>
                  <td className="px-4 py-3.5 text-right font-bold text-purple-600">
                    {currency(item.netBalance)}
                  </td>
                </tr>
              ))
            )}
          </tbody>
        </table>
      </div>
    </Card>
  );
}
