"use client"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import { Drawer, DrawerClose, DrawerContent, DrawerFooter, DrawerHeader, DrawerTrigger } from "@/components/ui/drawer"
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { useSortable } from "@dnd-kit/sortable"
import { IconGripVertical, IconHandClick } from "@tabler/icons-react"
import { useIsMobile } from "@/hooks/use-mobile"
import { Separator } from "@/components/ui/separator"
import { ColumnDef, flexRender, Row } from "@tanstack/react-table"
import { TableCell, TableRow } from "@/components/ui/table"
import { CSS } from "@dnd-kit/utilities"
import { schema, schemaType } from "./schema"
import { dateToLocaleString } from "@/lib/utils"
import { Pencil, Trash, TextSelect } from "lucide-react"

export function DraggableRow({ row }: { row: Row<z.infer<typeof schema>> }) {
    const { transform, transition, setNodeRef, isDragging } = useSortable({ id: row.original._id })
    return (
        <TableRow data-state={row.getIsSelected() && "selected"} data-dragging={isDragging} ref={setNodeRef}
            className="relative z-0 data-[dragging=true]:z-10 data-[dragging=true]:opacity-80"
            style={{ transform: CSS.Transform.toString(transform), transition: transition }}
        >
            {row.getVisibleCells().map((cell) => (
                <TableCell key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</TableCell>
            ))}
        </TableRow>
    )
}


function getColumns(router: any, deleteHandler: (id: string) => void): ColumnDef<schemaType>[] {
    return [
        {
            accessorKey: "type",
            header: "Type",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "token",
            header: "Token",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "typeToken",
            header: "Type Token",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "description",
            header: "Description",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "isProperty",
            header: "Is Property",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "expiryStarts",
            header: "Expiry Starts",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            accessorKey: "expiryEnds",
            header: "Expiry Ends",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            accessorKey: "createdAt",
            header: "Created At",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            accessorKey: "updateAt",
            header: "Updated At",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            id: "actions",
            header: "Actions",
            cell: ({ row }) => {
                return (
                    <div className="flex flex-row gap-2">
                        <Button className="bg-amber-400 text-black border-amber-400" variant="outline" size="sm" onClick={() => { router.push(`/user-types/update?uuid=${row.original.uuid}`) }}>
                            <Pencil />
                        </Button>
                        <Button className="bg-red-700 text-white border-red-700" variant="outline" size="sm" onClick={() => { deleteHandler(row.original.uuid) }}>
                            <Trash />
                        </Button>
                    </div>
                );
            },
        }
    ]
}


export { getColumns };