"use client";

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

interface MaintenanceReportTableProps {
  data: MaintenanceReportResponse;
  isLoading?: boolean;
}

export function MaintenanceReportTable({ data, isLoading = false }: MaintenanceReportTableProps) {
  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">
            <Wrench className="h-5 w-5 text-amber-600 dark:text-amber-400" />
            Relatório de Ordens de Serviço & Custos de Manutenção
          </h2>
          <p className="text-xs text-muted-foreground">
            Despesas com manutenção preventiva e corretiva por máquina e fornecedor.
          </p>
        </div>

        <div className="flex items-center gap-4 text-xs">
          <div className="rounded-lg bg-amber-500/10 p-2.5 border border-amber-500/20">
            <span className="text-muted-foreground block">Total de OS:</span>
            <span className="text-sm font-bold text-amber-600 dark:text-amber-400">
              {data.summary.totalOrders} ordens
            </span>
          </div>

          <div className="rounded-lg bg-rose-500/10 p-2.5 border border-rose-500/20">
            <span className="text-muted-foreground block">Custo Total de Manutenção:</span>
            <span className="text-sm font-bold text-rose-600 dark:text-rose-400">
              {currency(data.summary.totalCost)}
            </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">Código OS</th>
              <th className="px-4 py-3.5 font-medium">Título da Manutenção</th>
              <th className="px-4 py-3.5 font-medium">Equipamento</th>
              <th className="px-4 py-3.5 font-medium">Oficina / Fornecedor</th>
              <th className="px-4 py-3.5 font-medium">Abertura</th>
              <th className="px-4 py-3.5 font-medium text-right">Custo (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={7} className="p-8 text-center text-muted-foreground">
                  Carregando relatório de manutenção...
                </td>
              </tr>
            ) : data.items.length === 0 ? (
              <tr>
                <td colSpan={7} className="p-8 text-center text-muted-foreground">
                  Nenhuma ordem de serviço registrada no período.
                </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-medium text-foreground">
                    {item.title}
                  </td>
                  <td className="px-4 py-3.5 text-xs text-muted-foreground">
                    {item.equipmentName}
                  </td>
                  <td className="px-4 py-3.5 text-xs font-semibold text-foreground">
                    {item.supplierName}
                  </td>
                  <td className="px-4 py-3.5 font-mono text-xs text-muted-foreground">
                    {formatDate(item.openedAt)}
                  </td>
                  <td className="px-4 py-3.5 text-right font-bold text-rose-600">
                    {currency(item.cost)}
                  </td>
                  <td className="px-4 py-3.5 text-center">
                    <Badge
                      tone={
                        item.status === "DONE"
                          ? "green"
                          : item.status === "IN_PROGRESS"
                          ? "blue"
                          : item.status === "OPEN"
                          ? "yellow"
                          : "neutral"
                      }
                    >
                      {item.status === "DONE"
                        ? "CONCLUÍDA"
                        : item.status === "IN_PROGRESS"
                        ? "EM ANDAMENTO"
                        : item.status === "OPEN"
                        ? "ABERTA"
                        : "CANCELADA"}
                    </Badge>
                  </td>
                </tr>
              ))
            )}
          </tbody>
        </table>
      </div>
    </Card>
  );
}
