"use client"

import * as React from "react"
import { ColumnDef } from "@tanstack/react-table"
import { Checkbox } from "@/components/ui/checkbox"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { Edit, Ellipsis } from "lucide-react"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { formatDate } from "@/lib/utils"

// Import types
import { type DataTableRowAction } from "@/types/table"
import { 
  vendorInvestigationsColumnsConfig, 
  VendorInvestigationsViewWithContacts 
} from "@/config/vendorInvestigationsColumnsConfig"

// Props for the column generator function
interface GetVendorInvestigationsColumnsProps {
  setRowAction?: React.Dispatch<
    React.SetStateAction<
      DataTableRowAction<VendorInvestigationsViewWithContacts> | null
    >
  >
  openVendorDetailsModal?: (vendorId: number) => void
}

export function getColumns({
  setRowAction,
  openVendorDetailsModal,
}: GetVendorInvestigationsColumnsProps): ColumnDef<
  VendorInvestigationsViewWithContacts
>[] {
  // --------------------------------------------
  // 1) Select (checkbox) column
  // --------------------------------------------
  const selectColumn: ColumnDef<VendorInvestigationsViewWithContacts> = {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={
          table.getIsAllPageRowsSelected() ||
          (table.getIsSomePageRowsSelected() && "indeterminate")
        }
        onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
        aria-label="Select all"
        className="translate-y-0.5"
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value) => row.toggleSelected(!!value)}
        aria-label="Select row"
        className="translate-y-0.5"
      />
    ),
    size: 40,
    enableSorting: false,
    enableHiding: false,
  }

  // --------------------------------------------
  // 2) Actions column
  // --------------------------------------------
  const actionsColumn: ColumnDef<VendorInvestigationsViewWithContacts> = {
    id: "actions",
    enableHiding: false,
    cell: ({ row }) => {
      return (
        <Button
          variant="ghost"
          className="flex size-8 p-0 data-[state=open]:bg-muted"
          aria-label="실사 정보 수정"
          onClick={() => {
            setRowAction?.({ type: "update", row })
          }}
        >
          <Edit className="size-4" aria-hidden="true" />
        </Button>
      )
    },
    size: 40,
  }

  // --------------------------------------------
  // 3) Vendor Name with click handler
  // --------------------------------------------
  const vendorNameColumn: ColumnDef<VendorInvestigationsViewWithContacts> = {
    accessorKey: "vendorName",
    header: ({ column }) => (
      <DataTableColumnHeaderSimple column={column} title="협력사명" />
    ),
    cell: ({ row }) => {
      const vendorId = row.original.vendorId
      const vendorName = row.getValue("vendorName") as string

      return (
        <Button
          variant="link"
          className="p-0 h-auto font-normal"
          onClick={() => openVendorDetailsModal?.(vendorId)}
        >
          {vendorName}
        </Button>
      )
    },
    meta: {
      excelHeader: "협력사명",
      group: "협력업체",
    },
  }

  // --------------------------------------------
  // 4) Build grouped columns from config
  // --------------------------------------------
  const groupMap: Record<string, ColumnDef<VendorInvestigationsViewWithContacts>[]> = {}

  vendorInvestigationsColumnsConfig.forEach((cfg) => {
    // Skip vendorName as we have a custom column for it
    if (cfg.id === "vendorName") {
      return
    }

    const groupName = cfg.group || "_noGroup"

    if (!groupMap[groupName]) {
      groupMap[groupName] = []
    }

    const childCol: ColumnDef<VendorInvestigationsViewWithContacts> = {
      accessorKey: cfg.id,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title={cfg.label} />
      ),
      meta: {
        excelHeader: cfg.excelHeader,
        group: cfg.group,
        type: cfg.type,
      },
      cell: ({ row, column }) => {
        const value = row.getValue(column.id)
        
        // Handle date fields
        if (
          column.id === "scheduledStartAt" ||
          column.id === "scheduledEndAt" ||
          column.id === "forecastedAt" ||
          column.id === "requestedAt" ||
          column.id === "confirmedAt" ||
          column.id === "completedAt" ||
          column.id === "createdAt" ||
          column.id === "updatedAt"
        ) {
          if (!value) return ""
          return formatDate(new Date(value as string), "KR")
        }

        // Handle status fields with badges
        if (column.id === "investigationStatus") {
          if (!value) return ""
          
          return (
            <Badge variant={getStatusVariant(value as string)}>
              {formatStatus(value as string)}
            </Badge>
          )
        }

        // Handle evaluation result
        if (column.id === "evaluationResult") {
          if (!value) return ""
          
          return (
            <Badge variant={getResultVariant(value as string)}>
              {formatEnumValue(value as string)}
            </Badge>
          )
        }

        // Handle pqItems
        if (column.id === "pqItems") {
          if (!value) return <span className="text-muted-foreground">-</span>
            const items = typeof value === 'string' ? JSON.parse(value as string) : value;
            if (Array.isArray(items) && items.length > 0) {
              const firstItem = items[0];
              return (
                <div className="flex items-center gap-2">
                  <span className="text-sm">{firstItem.itemCode} - {firstItem.itemName}</span>
                  {items.length > 1 && (
                    <span className="text-xs text-muted-foreground">외 {items.length - 1}건</span>
                  )}
                </div>
              );
            }
        }

        // Handle IDs for pqSubmissionId (keeping for reference)
        if (column.id === "pqSubmissionId") {
          return value ? `#${value}` : ""
        }

        // Handle file attachment status
        if (column.id === "hasAttachments") {
          return (
            <div className="flex items-center justify-center">
              {value ? (
                <Badge variant="default" className="text-xs">
                  📎 첨부
                </Badge>
              ) : (
                <span className="text-muted-foreground text-xs">-</span>
              )}
            </div>
          )
        }

        if (column.id === "requesterName") {
          if (!value && !row.original.requesterEmail) {
            return <span className="text-muted-foreground">미배정</span>
          }
          
          return (
            <div className="flex flex-col">
              <span>{value || "미배정"}</span>
              {row.original.requesterEmail && (
                <span className="text-xs text-muted-foreground">{row.original.requesterEmail}</span>
              )}
            </div>
          )
        }

        if (column.id === "qmManagerName") {
          if (!value && !row.original.qmManagerEmail) {
            return <span className="text-muted-foreground">미배정</span>
          }
          
          return (
            <div className="flex flex-col">
              <span>{value || "미배정"}</span>
              {row.original.qmManagerEmail && (
                <span className="text-xs text-muted-foreground">{row.original.qmManagerEmail}</span>
              )}
            </div>
          )
        }

        return value ?? ""
      },
    }

    groupMap[groupName].push(childCol)
  })

  // Insert custom vendorNameColumn in the 협력업체 group
  if (groupMap["협력업체"]) {
    groupMap["협력업체"].unshift(vendorNameColumn)
  }

  // Convert the groupMap into nested columns
  const nestedColumns: ColumnDef<VendorInvestigationsViewWithContacts>[] = []
  for (const [groupName, colDefs] of Object.entries(groupMap)) {
    if (groupName === "_noGroup") {
      nestedColumns.push(...colDefs)
    } else {
      nestedColumns.push({
        id: groupName,
        header: groupName,
        columns: colDefs,
      })
    }
  }

  // --------------------------------------------
  // 5) Return final columns array (simplified)
  // --------------------------------------------
  return [
    selectColumn,
    ...nestedColumns,
    actionsColumn,
  ]
}

// Helper functions for formatting
function formatStatus(status: string): string {
  switch (status) {
    case "PLANNED":
      return "계획됨"
    case "IN_PROGRESS":
      return "진행 중"
    case "COMPLETED":
      return "완료됨"
    case "CANCELED":
      return "취소됨"
    case "RESULT_SENT":
      return "실사결과발송"
    default:
      return status
  }
}

function formatEnumValue(value: string): string {
  switch (value) {
    // Evaluation types
    case "PURCHASE_SELF_EVAL":
      return "구매자체평가"
    case "DOCUMENT_EVAL":
      return "서류평가"
    case "PRODUCT_INSPECTION":
      return "제품검사평가"
    case "SITE_VISIT_EVAL":
      return "방문실사평가"
    
    // Evaluation results
    case "APPROVED":
      return "승인"
    case "SUPPLEMENT":
      return "보완"
    case "REJECTED":
      return "불가"
    
    default:
      return value.replace(/_/g, " ").toLowerCase()
  }
}

function getStatusVariant(status: string): "default" | "secondary" | "outline" | "destructive" {
  switch (status) {
    case "PLANNED":
      return "secondary"
    case "IN_PROGRESS":
      return "default"
    case "COMPLETED":
      return "outline"
    case "CANCELED":
      return "destructive"
    default:
      return "default"
  }
}

function getResultVariant(result: string): "default" | "secondary" | "outline" | "destructive" {
  switch (result) {
    case "APPROVED":
      return "default"
    case "SUPPLEMENT":
      return "secondary"
    case "REJECTED":
      return "destructive"
    default:
      return "outline"
  }
}