"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"

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-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(router: any, deleteHandler: (id: string) => void, buildID: string): ColumnDef<schemaType>[] {
    return [
        {
            accessorKey: "uuid",
            header: "UUID",
        },
        {
            accessorKey: "person.firstName",
            header: "First Name",
        },
        {
            accessorKey: "person.middleName",
            header: "Middle Name",
        },
        {
            accessorKey: "person.surname",
            header: "Surname",
        },
        {
            accessorKey: "person.birthDate",
            header: "Birth Date",
            cell: ({ getValue }) => dateToLocaleString(getValue() as string),
        },
        {
            accessorKey: "build.info.buildName",
            header: "Build Name",
        },
        {
            accessorKey: "build.info.buildNo",
            header: "Build No",
        },
        {
            accessorKey: "part.no",
            header: "Part No",
        },
        {
            accessorKey: "part.level",
            header: "Level",
        },
        {
            accessorKey: "part.humanLivability",
            header: "Livability",
            cell: ({ getValue }) => getValue() ? <div className="text-green-500 px-2 py-1 rounded">Yes</div> : <div className="text-red-500 px-2 py-1 rounded">No</div>
        },
        {
            accessorKey: "person.birthPlace",
            header: "Birth Place",
        },
        {
            accessorKey: "userType.description",
            header: "User Type",
        },
        {
            accessorKey: "userType.isProperty",
            header: "Property",
            cell: ({ getValue }) => getValue() ? <div className="text-green-500 px-2 py-1 rounded">Yes</div> : <div className="text-red-500 px-2 py-1 rounded">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 (
                    <div>
                        <Button className="bg-amber-400 text-black border-amber-400" variant="outline" size="sm" onClick={() => { router.push(`/living-spaces/update?uuid=${row.original.uuid}&buildID=${buildID}`) }}>
                            <Pencil />
                        </Button>
                        <Button className="bg-red-700 text-white border-red-700 mx-4" variant="outline" size="sm" onClick={() => { deleteHandler(buildID) }}>
                            <Trash />
                        </Button>
                    </div>
                );
            },
        }
    ]
}

export { getColumns };