"use client"

import { use } from "react"
import { ResourcePage } from "@/shared/data-view/resource-page"
import { ColumnHeader } from "@/shared/data-view/table-view"
import FormDialog from "@/shared/components/form-dialog"
import { TaskForm } from "@/modules/tasks/task-form"
import { TASK_ROUTES, TaskStatus } from "@garage/api"
import type { TasksClient } from "@garage/api"
import { Badge } from "@/shared/components/ui/badge"
import { useJobCard } from "@/modules/job-cards/job-card-context"
import { formatDate, formatEnum } from "@/shared/utils/formatters"

export default function JobCardTasksPage({
    params,
}: {
    params: Promise<{ id: string }>
}) {
    const { id: jobCardId } = use(params)
    const jobCard = useJobCard()

    const defaultJobCard = jobCard
        ? { value: String((jobCard as any).id), label: (jobCard as any).title || `Job Card` }
        : null

    return (
        <ResourcePage<TasksClient>
            routeKey={TASK_ROUTES.INDEX}
            searchable
            searchPlaceholder="Search tasks..."
            statusFilter={{ statuses: TaskStatus }}
            getClient={(api) => api.tasks}
            extraParams={{ job_card_id: jobCardId }}
            header={null}
            tableHeader={({ invalidateQuery, selectedItem, closeDialog }) => (
                <div className="flex justify-end">
                    <FormDialog title="Task">
                        {(resourceId) => (
                            <TaskForm
                                resourceId={resourceId}
                                initialData={selectedItem ?? { job_card: defaultJobCard }}
                                onSuccess={() => { closeDialog(); invalidateQuery() }}
                            />
                        )}
                    </FormDialog>
                </div>
            )}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "task_number",
                    header: ({ column }) => <ColumnHeader column={column} title="Task #" />,
                    cell: ({ row }) => (row.original as any).task_number || "—",
                },
                {
                    accessorKey: "subject",
                    header: ({ column }) => <ColumnHeader column={column} title="Subject" />,
                    cell: ({ row }) => (row.original as any).subject || "—",
                },
                {
                    accessorKey: "due_date",
                    header: ({ column }) => <ColumnHeader column={column} title="Due Date" />,
                    cell: ({ row }) => formatDate((row.original as any).due_date),
                },
                {
                    accessorKey: "priority",
                    header: ({ column }) => <ColumnHeader column={column} title="Priority" />,
          
                },
                {
                    accessorKey: "status",
                    header: ({ column }) => <ColumnHeader column={column} title="Status" />,
               
                },
                actionsColumn(),
            ]}
        />
    )
}
