"use client";

import { ColumnDef } from "@tanstack/react-table";
import { EuResponsiblePersonDTO } from "@/lib/db/schemas/api";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { MoreHorizontal, Edit, Trash } from "lucide-react";
import { deleteEuResponsiblePerson } from "@/actions/settings";
import { toast } from "sonner";
import { formatDate } from "@/lib/format";

export type EuResponsiblePersonsOverview = EuResponsiblePersonDTO;

export const getColumns = (
  handleRowClick: (person: EuResponsiblePersonsOverview) => void,
  router: any,
  startTransition: (scope: () => void) => void,
  isPending: boolean
): ColumnDef<EuResponsiblePersonsOverview>[] => [
  {
    accessorKey: "id",
    header: "ID",
    cell: ({ row }) => (
      <span className="text-xs text-muted-foreground font-mono">
        #{row.getValue("id")}
      </span>
    ),
  },
  {
    accessorKey: "companyName",
    header: "Firmenname",
  },
  {
    accessorKey: "email",
    header: "Email",
  },
  {
    accessorKey: "phoneNumber",
    header: "Telefonnummer",
  },
  {
    accessorKey: "address",
    header: "Adresse",
  },
  {
    accessorKey: "createdAt",
    header: "Erstellt am",
    cell: ({ row }) => {
      const date = (row.getValue("createdAt") || (row.original as any).created_at) as Date | string | null;
      if (!date) return "-";
      return formatDate(new Date(date));
    },
  },
  {
    id: "actions",
    cell: ({ row }) => {
      const person = row.original;

      const onDelete = async () => {
        const companyName = person!.companyName || (person as any).company_name;
        if (confirm(`Möchten Sie die EU Verantwortliche Person "${companyName}" wirklich löschen?`)) {
          startTransition(async () => {
            const result = await deleteEuResponsiblePerson({ id: person!.id });
            if (!result.success) {
              toast.error(result.error);
            } else {
              toast.success(result.message || "EU Verantwortliche Person erfolgreich gelöscht.");
              router.refresh();
            }
          });
        }
      };

      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button variant="ghost" className="h-8 w-8 p-0" disabled={isPending}>
              <span className="sr-only">Menü öffnen</span>
              <MoreHorizontal className="h-4 w-4" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end">
            <DropdownMenuLabel>Aktionen</DropdownMenuLabel>
            <DropdownMenuItem onClick={() => handleRowClick(person)}>
              <Edit className="mr-2 h-4 w-4" />
              Bearbeiten
            </DropdownMenuItem>
            <DropdownMenuItem onClick={onDelete} className="text-destructive">
              <Trash className="mr-2 h-4 w-4" />
              Löschen
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      );
    },
  },
];
