"use client"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import { useSortable } from "@dnd-kit/sortable"
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"
import { IconHandClick } from "@tabler/icons-react"

export function DraggableRow({ row, selectedID }: { row: Row<z.infer<typeof schema>>; selectedID: string }) {
    const { transform, transition, setNodeRef, isDragging } = useSortable({ id: row.original._id })
    return (
        <TableRow data-state={row.getIsSelected() && "selected"} data-dragging={isDragging} ref={setNodeRef}
            className={`${row.original._id === selectedID ? "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(selectionHandler: (id: string, token: string) => void): ColumnDef<schemaType>[] {
    return [
        {
            accessorKey: "_id",
            header: "ID",
        },
        {
            accessorKey: "buildType.token",
            header: "Token",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "collectionToken",
            header: "Collection Token",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.govAddressCode",
            header: "Gov Address Code",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.buildName",
            header: "Build Name",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.buildNo",
            header: "Build No",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.maxFloor",
            header: "Max Floor",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.undergroundFloor",
            header: "Underground Floor",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.buildDate",
            header: "Build Date",
            cell: ({ getValue }) => getValue() ? dateToLocaleString(getValue() as string) : "-",
        },
        {
            accessorKey: "info.decisionPeriodDate",
            header: "Decision Period Date",
            cell: ({ getValue }) => getValue() ? dateToLocaleString(getValue() as string) : "-",
        },
        {
            accessorKey: "info.taxNo",
            header: "Tax No",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.liftCount",
            header: "Lift Count",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "info.heatingSystem",
            header: "Heating System",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "info.coolingSystem",
            header: "Cooling System",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "info.hotWaterSystem",
            header: "Hot Water System",
            cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
        },
        {
            accessorKey: "info.blockServiceManCount",
            header: "Block Service Man Count",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.securityServiceManCount",
            header: "Security Service Man Count",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.garageCount",
            header: "Garage Count",
            cell: ({ getValue }) => getValue(),
        },
        {
            accessorKey: "info.managementRoomId",
            header: "Management Room ID",
            cell: ({ getValue }) => getValue(),
        },
        {
            id: "actions",
            header: "Actions",
            cell: ({ row }) => {
                return (
                    <div>
                        <Button className="bg-blue-600 border-blue-600 text-white" variant="outline" size="sm" onClick={() => { selectionHandler(row.original._id, row.original.collectionToken) }}>
                            <IconHandClick />
                        </Button>
                    </div>
                );
            },
        }
    ]
}

export { getColumns };