"use client"

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

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 { LIST_HREF } from "./constants"
import type { DerivativeMasterRow } from "./schema"

const includesStringFilter: FilterFn<DerivativeMasterRow> = (
  row,
  columnId,
  value
) => {
  if (!value) return true
  return String(row.getValue(columnId) ?? "")
    .toLowerCase()
    .includes(String(value).toLowerCase())
}

export function createDerivativeMasterColumns(
  onDelete?: (srNo: string) => void
): ColumnDef<DerivativeMasterRow>[] {
  return [
    {
      accessorKey: "srNo",
      header: "Sr No",
      enableColumnFilter: false,
    },
    {
      accessorKey: "parentIsin",
      header: "Parent ISIN",
      filterFn: includesStringFilter,
      cell: ({ row }) => (
        <span className="font-mono text-sm">{row.original.parentIsin}</span>
      ),
    },
    {
      accessorKey: "currency",
      header: "Currency",
      filterFn: includesStringFilter,
    },
    {
      accessorKey: "marketPrice",
      header: "Market Price",
      filterFn: includesStringFilter,
    },
    {
      id: "options",
      header: () => <span className="block w-full text-right">Options</span>,
      cell: ({ row }) => (
        <div className="flex justify-end gap-1">
          <Tooltip>
            <TooltipTrigger asChild>
              <Button variant="secondary" size="icon-sm" className="size-7" asChild>
                <Link href={`${LIST_HREF}/${encodeURIComponent(row.original.srNo)}`}>
                  <Pencil className="size-3.5" />
                </Link>
              </Button>
            </TooltipTrigger>
            <TooltipContent>Update</TooltipContent>
          </Tooltip>
          <AlertDialog>
            <AlertDialogTrigger asChild>
              <Button variant="destructive" size="icon-sm" className="size-7">
                <Trash2 className="size-3.5" />
              </Button>
            </AlertDialogTrigger>
            <AlertDialogContent>
              <AlertDialogHeader>
                <AlertDialogTitle>Delete record?</AlertDialogTitle>
                <AlertDialogDescription>
                  Remove derivative master {row.original.srNo}? This cannot be undone.
                </AlertDialogDescription>
              </AlertDialogHeader>
              <AlertDialogFooter>
                <AlertDialogCancel>Cancel</AlertDialogCancel>
                <AlertDialogAction
                  onClick={() => {
                    onDelete?.(row.original.srNo)
                    toast.success("Deleted")
                  }}
                >
                  Delete
                </AlertDialogAction>
              </AlertDialogFooter>
            </AlertDialogContent>
          </AlertDialog>
        </div>
      ),
      enableSorting: false,
    },
  ]
}
