"use client";

import { CheckCircle2 } from "lucide-react";
import { useRepositories } from "@/di/repository-provider";
import { useApiMutation } from "@/presentation/hooks/use-api-mutation";
import { useT, useLocale } from "@/shared/i18n/client";
import { formatDate } from "@/shared/lib/format-date";
import { Button } from "@/presentation/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/presentation/components/ui/card";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/presentation/components/ui/table";
import type { DataQualityIssue } from "@/domain/entities/data-quality";

export default function FlaggedVehiclesCard({
  issues,
  client,
  onSelectIssue,
  onChanged,
}: {
  issues: DataQualityIssue[];
  client: string;
  onSelectIssue: (id: number) => void;
  onChanged: () => void;
}) {
  const t = useT();
  const locale = useLocale();
  const copy = t.dataQuality;
  const { dataQualityRepository } = useRepositories();
  const nf = new Intl.NumberFormat(locale === "fr" ? "fr-FR" : "en-US");

  const rows = issues.flatMap((issue) =>
    issue.vehicles
      .filter((v) => client === "all" || v.client === client)
      .map((v) => ({ ...v, issue }))
  );

  const resolveMutation = useApiMutation(
    (linkId: number) => dataQualityRepository.resolveVehicle(linkId),
    { successMessage: copy.toasts.vehicleResolved, onSuccess: onChanged }
  );

  return (
    <Card className="gap-4">
      <CardHeader>
        <CardTitle>{copy.flagged.title}</CardTitle>
        <CardDescription>{copy.flagged.subtitle}</CardDescription>
      </CardHeader>
      <CardContent>
        <div className="rounded-lg border">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>{copy.detail.columns.plate}</TableHead>
                <TableHead>{copy.detail.columns.client}</TableHead>
                <TableHead>{copy.detail.columns.box}</TableHead>
                <TableHead>{copy.flagged.issue}</TableHead>
                <TableHead>{copy.detail.columns.measure}</TableHead>
                <TableHead>{copy.detail.columns.since}</TableHead>
                <TableHead className="text-right">{copy.detail.columns.km}</TableHead>
                <TableHead className="w-10" />
              </TableRow>
            </TableHeader>
            <TableBody>
              {rows.length === 0 ? (
                <TableRow>
                  <TableCell colSpan={8} className="py-6 text-center text-sm text-muted-foreground">{copy.flagged.empty}</TableCell>
                </TableRow>
              ) : rows.map((v) => (
                <TableRow key={v.link_id}>
                  <TableCell>
                    <span className="font-medium">{v.plate}</span>
                    {v.model && <span className="block text-xs text-muted-foreground">{v.model}</span>}
                  </TableCell>
                  <TableCell>{v.client ?? "—"}</TableCell>
                  <TableCell>{v.box ?? "—"}</TableCell>
                  <TableCell className="whitespace-normal">
                    <button type="button" className="text-left text-primary hover:underline" onClick={() => onSelectIssue(v.issue.id)}>
                      {v.issue.title}
                    </button>
                  </TableCell>
                  <TableCell className="whitespace-normal">{v.measure ?? "—"}</TableCell>
                  <TableCell>{v.since ? formatDate(v.since, locale) : "—"}</TableCell>
                  <TableCell className="text-right font-mono">{v.km_30d === null ? "—" : nf.format(v.km_30d)}</TableCell>
                  <TableCell>
                    <Button variant="ghost" size="sm" disabled={resolveMutation.loading} onClick={() => resolveMutation.mutate(v.link_id)}>
                      <CheckCircle2 className="w-4 h-4 mr-1.5" />
                      {copy.detail.resolve}
                    </Button>
                  </TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </div>
      </CardContent>
    </Card>
  );
}
