"use client";

import { ColumnDef } from "@tanstack/react-table";
import { ShipmentTrackingDTOSchema } from "@/lib/db/schemas/api";
import { z } from "zod";
import { formatDate } from "@/lib/format";
import { Button } from "@/components/ui/button";
import { Edit, Trash2 } from "lucide-react";

export type ShipmentsOverview = z.infer<typeof ShipmentTrackingDTOSchema>;

export const columns: ColumnDef<ShipmentsOverview>[] = [
  {
    accessorKey: "trackingNumber",
    header: "Sendungsnummer",
  },
  {
    accessorKey: "carrier",
    header: "Versanddienstleister",
  },
  {
    accessorKey: "shipmentType",
    header: "Typ",
  },
  {
    accessorKey: "status",
    header: "Status",
  },
  {
    accessorKey: "createdAt",
    header: "Erstellt am",
    cell: ({ row }) => {
      const date = row.getValue("createdAt") as Date | null;
      if (!date) return "-";
      return formatDate(date);
    },
  },
  {
    id: "actions",
    header: "Aktionen",
    cell: ({ row, table }) => {
      const shipment = row.original;
      const meta = table.options.meta as any;

      return (
        <div className="flex items-center gap-2">
          <Button
            variant="outline"
            size="sm"
            onClick={() => meta?.onEdit?.(shipment)}
          >
            <Edit className="h-4 w-4" />
          </Button>
          <Button
            variant="outline"
            size="sm"
            onClick={() => meta?.onDelete?.(shipment)}
          >
            <Trash2 className="h-4 w-4" />
          </Button>
        </div>
      );
    },
  },
];
