"use client"

import { CrudDialog } from "@/shared/components/crud-dialog"
import { ColumnHeader } from "@/shared/data-view/table-view"
import { useAuthApi } from "@/shared/useApi"
import { TASK_TYPE_ROUTES } from "@garage/api"
import { TaskTypeForm } from "./task-type-form"

export function TaskTypeCrudDialog() {
    const api = useAuthApi()

    return (
        <CrudDialog
            title="Task Type"
            queryKey={[TASK_TYPE_ROUTES.INDEX]}
            getClient={() => api.taskTypes}
            resourceLabel="task type"
            columns={() => [
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                },
                {
                    accessorKey: "is_default",
                    header: () => <span>Default</span>,
                    cell: ({ row }) => ((row.original as any).is_default ? "Yes" : "No"),
                    enableSorting: false,
                },
            ]}
            renderForm={({ resourceId, initialData, onSuccess }) => (
                <TaskTypeForm
                    resourceId={resourceId}
                    initialData={initialData}
                    onSuccess={onSuccess}
                />
            )}
        />
    )
}
