"use client"

import * as React from "react"
import { ColumnDef } from "@tanstack/react-table"
import { formatDate, formatDateTime } from "@/lib/utils"
import { Checkbox } from "@/components/ui/checkbox"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { DataTableRowAction } from "@/types/table"
import { VendorDocumentsView } from "@/db/schema/vendorDocu"

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<VendorDocumentsView> | null>>
}

export function getColumns({
  setRowAction,
}: GetColumnsProps): ColumnDef<VendorDocumentsView>[] {
  return [
    {
      id: "select",
      // Remove the "Select all" checkbox in header since we're doing single-select
      header: () => <span className="sr-only">Select</span>,
      cell: ({ row, table }) => (
        <Checkbox
          checked={row.getIsSelected()}
          onCheckedChange={(value) => {
            // If selecting this row
            if (value) {
              // First deselect all rows (to ensure single selection)
              table.toggleAllRowsSelected(false)
              // Then select just this row
              row.toggleSelected(true)
              // Trigger the same action that was in the "Select" button
              setRowAction({ row, type: "select" })
            } else {
              // Just deselect this row
              row.toggleSelected(false)
            }
          }}
          aria-label="Select row"
          className="translate-y-0.5"
        />
      ),
      enableSorting: false,
      enableHiding: false,
      enableResizing: false,
      size: 40,
      minSize: 40,
      maxSize: 40,
    },
    
    {
      accessorKey: "docNumber",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Doc Number" />
      ),
      cell: ({ row }) => <div>{row.getValue("docNumber")}</div>,
      meta: {
        excelHeader: "Doc Number"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },
    {
      accessorKey: "title",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Doc title" />
      ),
      cell: ({ row }) => <div>{row.getValue("title")}</div>,
      meta: {
        excelHeader: "Doc title"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },
    {
      accessorKey: "pic",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="SHI PIC" />
      ),
      cell: ({ row }) => <div>{row.getValue("pic")}</div>,
      meta: {
        excelHeader: "SHI PIC"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },
    {
      accessorKey: "latestStageName",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Latest Stage Name" />
      ),
      cell: ({ row }) => <div>{row.getValue("latestStageName")}</div>,
      meta: {
        excelHeader: "Latest Stage Name"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },
    {
      accessorKey: "latestStagePlanDate",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Latest Stage Plan Date" />
      ),
      cell: ({ cell }) => {
        const value = cell.getValue();
        return value ? formatDate(value as Date) : "";
      },      meta: {
        excelHeader: "Latest Stage Plan Date"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },
    {
      accessorKey: "latestStageActualDate",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Latest Stage Actual Date" />
      ),
      cell: ({ cell }) => {
        const value = cell.getValue();
        return value ? formatDate(value as Date) : "";
      },      meta: {
        excelHeader: "Latest Stage Actual Date"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },
    {
      accessorKey: "latestRevision",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Latest Revision" />
      ),
      cell: ({ row }) => <div>{row.getValue("latestRevision")}</div>,
      meta: {
        excelHeader: "Latest Revision"
      },
      enableResizing: true,
      minSize: 100,
      size: 160,
    },

    {
      accessorKey: "updatedAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Updated At" />
      ),
      cell: ({ cell }) => formatDateTime(cell.getValue() as Date),
      meta: {
        excelHeader: "updated At"
      },
      enableResizing: true,
      minSize: 120,
      size: 180,
    },
    // The "actions" column has been removed
  ]
}