"use client"

import * as React from "react"
import { ColumnDef } from "@tanstack/react-table"
import { formatDate } from "@/lib/utils"
import { Checkbox } from "@/components/ui/checkbox"
import { Badge } from "@/components/ui/badge"
import {
} from "@/components/ui/dropdown-menu"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"

// Accepted Quotation 타입 정의
export interface AcceptedQuotationItem {
  id: number
  rfqId: number
  vendorId: number
  quotationCode: string | null
  quotationVersion: number | null
  totalPrice: string | null
  currency: string | null
  validUntil: Date | null
  status: string
  remark: string | null
  submittedAt: Date | null
  acceptedAt: Date | null
  createdAt: Date
  updatedAt: Date
  
  // RFQ 정보
  rfqCode: string | null
  rfqType: string | null
  description: string | null
  dueDate: Date | null
  rfqStatus: string | null
  materialCode: string | null
  
  // Vendor 정보
  vendorName: string
  vendorCode: string | null
  vendorEmail: string | null
  vendorCountry: string | null
  
  // Project 정보
  projNm: string | null
  pspid: string | null
  sector: string | null
}

export function getColumns(): ColumnDef<AcceptedQuotationItem>[] {
  // ----------------------------------------------------------------
  // 1) select 컬럼 (체크박스)
  // ----------------------------------------------------------------
  const selectColumn: ColumnDef<AcceptedQuotationItem> = {
    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 컬럼 (Dropdown 메뉴)
  // ----------------------------------------------------------------
  // const actionsColumn: ColumnDef<AcceptedQuotationItem> = {
  //   id: "actions",
  //   cell: ({ row }) => (
  //     <DropdownMenu>
  //       <DropdownMenuTrigger asChild>
  //         <Button
  //           aria-label="Open menu"
  //           variant="ghost"
  //           className="flex size-8 p-0 data-[state=open]:bg-muted"
  //         >
  //           <Ellipsis className="size-4" aria-hidden="true" />
  //         </Button>
  //       </DropdownMenuTrigger>
  //       <DropdownMenuContent align="end" className="w-40">
  //         <DropdownMenuItem
  //           onSelect={() => setRowAction({ row, type: "open" })}
  //         >
  //           견적서 보기
  //         </DropdownMenuItem>
  //       </DropdownMenuContent>
  //     </DropdownMenu>
  //   ),
  //   size: 40,
  //   enableSorting: false,
  //   enableHiding: false,
  // }

  // ----------------------------------------------------------------
  // 3) 데이터 컬럼들 정의
  // ----------------------------------------------------------------
  const dataColumns: ColumnDef<AcceptedQuotationItem>[] = [
    {
      accessorKey: "rfqCode",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="RFQ 코드" />
      ),
      cell: ({ row }) => (
        <div className="font-medium">
          {row.original.rfqCode || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "RFQ 코드",
      },
    },
    {
      accessorKey: "description",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="RFQ 설명" />
      ),
      cell: ({ row }) => (
        <div className="max-w-32 truncate">
          {row.original.description || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "RFQ 설명",
      },
    },
    {
      accessorKey: "rfqType",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="RFQ 타입" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.rfqType || "-"}
        </div>
      ),
      enableSorting: true,
    },
    {
      accessorKey: "vendorName",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="업체명" />
      ),
      cell: ({ row }) => (
        <div className="font-medium">
          {row.original.vendorName}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "업체명",
      },
    },
    {
      accessorKey: "vendorCode",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="업체 코드" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.vendorCode || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "업체 코드",
      },
    },
    {
      accessorKey: "quotationCode",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="견적서 코드" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.quotationCode || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "견적서 코드",
      },
    },
    {
      accessorKey: "totalPrice",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="총 금액" />
      ),
      cell: ({ row }) => {
        const price = row.original.totalPrice;
        const currency = row.original.currency || "USD";
        return (
          <div className="text-right font-medium">
            {price ? `${Number(price).toLocaleString()} ${currency}` : "-"}
          </div>
        );
      },
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "총 금액",
      },
    },
    {
      accessorKey: "status",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="상태" />
      ),
      cell: ({ row }) => (
        <Badge variant="default" className="bg-green-100 text-green-800">
          {row.original.status}
        </Badge>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "상태",
      },
    },
    {
      accessorKey: "projNm",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트명" />
      ),
      cell: ({ row }) => (
        <div className="max-w-32 truncate">
          {row.original.projNm || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "프로젝트명",
      },
    },
    {
      accessorKey: "materialCode",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재 코드" />
      ),
      cell: ({ row }) => (
        <div className="max-w-32 truncate">
          {row.original.materialCode || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "자재 코드",
      },
    },
    {
      accessorKey: "vendorCountry",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="국가" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.vendorCountry || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "국가",
      },
    },
    {
      accessorKey: "dueDate",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="마감일" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.dueDate ? formatDate(row.original.dueDate) : "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "마감일",
      },
    },
    {
      accessorKey: "acceptedAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="승인일" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.acceptedAt ? formatDate(row.original.acceptedAt) : "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "승인일",
      },
    },
  ]

  return [selectColumn, ...dataColumns]
} 