"use client"

import { ResourcePage } from '@/shared/data-view/resource-page'
import { ColumnHeader } from '@/shared/data-view/table-view'
import FormDialog from '@/shared/components/form-dialog'
import { JobCardForm } from '@/modules/job-cards/job-card-form'
import { JOB_CARD_ROUTES, JobCardStatus } from '@garage/api'
import type { JobCardsClient } from '@garage/api'
import { Tabs, TabsList, TabsTrigger } from '@/shared/components/ui/tabs'
import { ClipboardListIcon, SearchIcon } from 'lucide-react'
import { Badge } from '@/shared/components/ui/badge'
import { Input } from '@/shared/components/ui/input'
import { useRouter } from 'next/navigation'
import { useState, useEffect } from 'react'

type JobCardItem = {
    id: number
    title?: string
    status?: string
    check_in_date?: string
    km_in?: number
    created_at?: string
}

const statusColorMap: Record<string, string> = {
    draft: "secondary",
    check_in: "default",
    in_progress: "default",
    completed: "default",
    invoiced: "outline",
    cancelled: "destructive",
}

const formatStatus = (status?: string) => {
    if (!status) return "—"
    return status
        .split("_")
        .map((w) => w.charAt(0).toUpperCase() + w.slice(1))
        .join(" ")
}

export default function JobCardsPage() {
    const router = useRouter()
    const [searchInput, setSearchInput] = useState("")
    const [search, setSearch] = useState("")
    const [statusFilter, setStatusFilter] = useState<string>("all")

    useEffect(() => {
        const timer = setTimeout(() => setSearch(searchInput), 400)
        return () => clearTimeout(timer)
    }, [searchInput])

    const extraParams: Record<string, unknown> = {}
    if (search) extraParams.search = search
    if (statusFilter !== "all") extraParams.status = statusFilter

    return (
        <ResourcePage<JobCardsClient>
            pageTitle="Job Cards"
            routeKey={JOB_CARD_ROUTES.INDEX}
            getClient={(api) => api.jobCards}
            extraParams={extraParams}
            onRowClick={(row) => router.push(`/sales/job-cards/${row.id}`)}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <FormDialog title="Job Card">
                        {(resourceId) => (
                            <JobCardForm
                                resourceId={resourceId}
                                initialData={selectedItem}
                                onSuccess={invalidateQuery}
                            />
                        )}
                    </FormDialog>
                ),
            })}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                    cell: ({ row }) => {
                        const item = row.original as unknown as JobCardItem
                        return (
                            <div className="flex items-center gap-2">
                                <ClipboardListIcon className="text-muted-foreground h-4 w-4" />
                                <span>{item.title}</span>
                            </div>
                        )
                    },
                },
                {
                    accessorKey: "status",
                    header: ({ column }) => <ColumnHeader column={column} title="Status" />,
                    cell: ({ row }) => {
                        const item = row.original as unknown as JobCardItem
                        return (
                            <Badge variant={statusColorMap[item.status ?? ""] as any ?? "outline"}>
                                {formatStatus(item.status)}
                            </Badge>
                        )
                    },
                },
                {
                    accessorKey: "check_in_date",
                    header: ({ column }) => <ColumnHeader column={column} title="Check-in Date" />,
                },
                {
                    accessorKey: "km_in",
                    header: ({ column }) => <ColumnHeader column={column} title="KM In" />,
                    cell: ({ row }) => {
                        const item = row.original as unknown as JobCardItem
                        return item.km_in ? Number(item.km_in).toLocaleString() : "—"
                    },
                },
                {
                    accessorKey: "created_at",
                    header: ({ column }) => <ColumnHeader column={column} title="Created" />,
                    cell: ({ row }) => {
                        const item = row.original as unknown as JobCardItem
                        return item.created_at ? new Date(item.created_at).toLocaleDateString() : "—"
                    },
                },
                actionsColumn(),
            ]}
            toolbar={
                <div className="flex   gap-3 w-full">
             
                    <div className="relative w-64">
                        <SearchIcon className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
                        <Input
                            placeholder="Search job cards..."
                            value={searchInput}
                            onChange={(e) => setSearchInput(e.target.value)}
                            className="pl-8"
                        />
                    </div>
                </div>
            }
            tableHeader={
                       <Tabs value={statusFilter} onValueChange={setStatusFilter}>
                        <TabsList variant="default">
                            <TabsTrigger value="all" className='data-[state=active]:bg-primary/10 data-[state=active]:text-primary'>All</TabsTrigger>
                            {JobCardStatus.map((status) => (
                                <TabsTrigger className='data-[state=active]:bg-primary/10 data-[state=active]:text-primary  ' key={status} value={status}>
                                    {formatStatus(status)}
                                </TabsTrigger>
                            ))}
                        </TabsList>
                    </Tabs>
            }
        />
    )
}
