"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 { IconCircleMinus, 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 } from "lucide-react"


export function DraggableRow({ row, selectedIDs }: { row: Row<z.infer<typeof schema>>; selectedIDs: string[] }) {
    const { transform, transition, setNodeRef, isDragging } = useSortable({ id: row.original._id })
    return (
        <TableRow data-state={row.getIsSelected() && "selected"} data-dragging={isDragging} ref={setNodeRef}
            className={`${selectedIDs.includes(row.original._id) ? "bg-blue-700/50 hover:bg-blue-700/50" : ""} 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(appendCompanyID: (id: string) => void, removeCompanyID: (id: string) => void, selectedCompanyIDS: string[], defaultSelection: string, setDefaultSelection: (id: string) => void): ColumnDef<schemaType>[] {
    return [
        {
            accessorKey: "uuid",
            header: "UUID",
            cell: ({ getValue }) => (<div className="font-mono text-xs bg-muted px-2 py-1 rounded break-all">{String(getValue())}</div>),
        },
        {
            accessorKey: "formal_name",
            header: "Formal Name",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "company_type",
            header: "Company Type",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "commercial_type",
            header: "Commercial Type",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "tax_no",
            header: "Tax No",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "public_name",
            header: "Public Name",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "company_tag",
            header: "Company Tag",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "default_lang_type",
            header: "Default Language",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "default_money_type",
            header: "Default Money Type",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "is_commercial",
            header: "Is Commercial",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "is_blacklist",
            header: "Is Blacklist",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "createdAt",
            header: "Created",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            accessorKey: "updatedAt",
            header: "Updated",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            accessorKey: "expiryStarts",
            header: "Expiry Starts",
            cell: ({ getValue }) => getValue() ? dateToLocaleString(getValue() as string) : "-",
        },
        {
            accessorKey: "expiryEnds",
            header: "Expiry Ends",
            cell: ({ getValue }) => getValue() ? dateToLocaleString(getValue() as string) : "-",
        },
        {
            id: "actions",
            header: "Actions",
            cell: ({ row }) => {
                return (
                    selectedCompanyIDS.includes(row.original._id) ? (
                        <div>
                            <Button className="bg-blue-600 border-blue-600 text-white" variant="outline" size="sm" onClick={() => { appendCompanyID(row.original._id) }}>
                                <IconHandClick />
                            </Button>
                        </div>
                    ) :
                        <div>
                            <Button className="bg-blue-600 border-blue-600 text-white" variant="outline" size="sm" onClick={() => { removeCompanyID(row.original._id) }}>
                                <IconCircleMinus />
                            </Button>
                        </div>
                );
            },
        }
    ]
}

export { getColumns };