"use client"

import type { ColumnDef } from "@tanstack/react-table"
import { Pencil, Trash2 } from "lucide-react"
import Link from "next/link"

import { Button } from "@/components/ui/button"
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip"

import { buildUpdateHref } from "./constants"
import type { UnderlyingAliasRow } from "./schema"

export function createUnderlyingAliasColumns(
  onDelete?: (aliasCode: string) => void,
): ColumnDef<UnderlyingAliasRow>[] {
  return [
    {
      accessorKey: "aliasCode",
      header: "Alias code",
      cell: ({ row }) => (
        <span className="font-mono text-sm break-all">{row.original.aliasCode}</span>
      ),
    },
    {
      accessorKey: "aliasIsin",
      header: "Real ISIN",
      cell: ({ row }) => (
        <span className="font-mono text-sm">{row.original.aliasIsin || "—"}</span>
      ),
    },
    {
      accessorKey: "aliasSource",
      header: "Alias source",
      cell: ({ row }) => row.original.aliasSource || "—",
    },
    {
      accessorKey: "updatedSource",
      header: "Updated source",
      cell: ({ row }) => row.original.updatedSource || "—",
    },
    {
      accessorKey: "lastUpdated",
      header: "Last updated",
      cell: ({ row }) => row.original.lastUpdated || "—",
    },
    {
      id: "options",
      header: () => <span className="block w-full text-right">Options</span>,
      enableColumnFilter: false,
      enableSorting: false,
      cell: ({ row }) => (
        <div className="flex justify-end gap-1">
          <Tooltip>
            <TooltipTrigger asChild>
              <Button variant="secondary" size="icon-sm" className="size-7" asChild>
                <Link
                  href={buildUpdateHref(row.original.aliasCode)}
                  aria-label="Update underlying alias"
                >
                  <Pencil className="size-3.5" />
                </Link>
              </Button>
            </TooltipTrigger>
            <TooltipContent>Update</TooltipContent>
          </Tooltip>
          {onDelete ? (
            <AlertDialog>
              <AlertDialogTrigger asChild>
                <Button
                  variant="destructive"
                  size="icon-sm"
                  className="size-7"
                  aria-label="Delete underlying alias"
                >
                  <Trash2 className="size-3.5" />
                </Button>
              </AlertDialogTrigger>
              <AlertDialogContent>
                <AlertDialogHeader>
                  <AlertDialogTitle>Delete underlying alias?</AlertDialogTitle>
                  <AlertDialogDescription>
                    Remove alias &quot;{row.original.aliasCode}&quot;? This cannot be undone.
                  </AlertDialogDescription>
                </AlertDialogHeader>
                <AlertDialogFooter>
                  <AlertDialogCancel>Cancel</AlertDialogCancel>
                  <AlertDialogAction onClick={() => onDelete(row.original.aliasCode)}>
                    Delete
                  </AlertDialogAction>
                </AlertDialogFooter>
              </AlertDialogContent>
            </AlertDialog>
          ) : null}
        </div>
      ),
    },
  ]
}
