"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"

// RFQ 아이템 정보 타입
export interface RfqItemInfo {
  itemCode: string
  workType: string
  itemList: string
  subItemList: string
  shipTypes: string
}

// Accepted Quotation 타입 정의
export interface AcceptedQuotationItem {
  id: number
  rfqId: number
  vendorId: number
  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
  
  // RFQ 아이템 정보
  rfqItems: RfqItemInfo[]
  
  // 확장된 아이템 정보
  itemIndex: number
  totalItems: number
  isExpanded: boolean
  itemCode: string
  workType: string
  itemList: string
  subItemList: string
  shipTypes: string
}

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,
  }

  // ----------------------------------------------------------------
  // 3) 데이터 컬럼들 정의
  // ----------------------------------------------------------------
  const dataColumns: ColumnDef<AcceptedQuotationItem>[] = [
    // 프로젝트 관련 컬럼
    {
      accessorKey: "pspid",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트 코드" />
      ),
      cell: ({ row }) => (
        <div className="font-medium">
          {row.original.pspid || "-"}
        </div>
      ),
      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: "프로젝트명",
      },
    },
    // RFQ quotation 관련 컬럼
    {
      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,
      enableHiding: true,
      meta: {
        excelHeader: "RFQ 타입",
      },
    },
    {
      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: "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: "vendorCountry",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="업체 국가" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.vendorCountry || "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "업체 국가",
      },
    },
    // 아이템 관련 컬럼
    {
      accessorKey: "materialGroup",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재그룹" />
      ),
      cell: ({ row }) => {
        return (
          <div className="max-w-32 truncate">
            {row.original.itemCode || "-"}
          </div>
        );
      },
      enableSorting: false,
      enableHiding: true,
      meta: {
        excelHeader: "자재그룹",
      },
    },
    {
      accessorKey: "workType",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="공종" />
      ),
      cell: ({ row }) => {
        return (
          <div className="max-w-32 truncate">
            {row.original.workType || "-"}
          </div>
        );
      },
      enableSorting: false,
      enableHiding: true,
      meta: {
        excelHeader: "공종",
      },
    },
    {
      accessorKey: "shipType",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="선종" />
      ),
      cell: ({ row }) => {
        return (
          <div className="max-w-32 truncate">
            {row.original.shipTypes || "-"}
          </div>
        );
      },
      enableSorting: false,
      enableHiding: true,
      meta: {
        excelHeader: "선종",
      },
    },
    {
      accessorKey: "itemName",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재명" />
      ),
      cell: ({ row }) => {
        return (
          <div className="max-w-32 truncate">
            {row.original.itemList || "-"}
          </div>
        );
      },
      enableSorting: false,
      enableHiding: true,
      meta: {
        excelHeader: "자재명",
      },
    },
    {
      accessorKey: "itemDetail",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재명(상세)" />
      ),
      cell: ({ row }) => {
        return (
          <div className="max-w-32 truncate">
            {row.original.subItemList || "-"}
          </div>
        );
      },
      enableSorting: false,
      enableHiding: true,
      meta: {
        excelHeader: "자재명(상세)",
      },
    },
    // metadata 관련 컬럼
    {
      accessorKey: "dueDate",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="RFQ 마감일" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.dueDate ? formatDate(row.original.dueDate) : "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "RFQ 마감일",
      },
    },
    {
      accessorKey: "acceptedAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="RFQ 승인일" />
      ),
      cell: ({ row }) => (
        <div>
          {row.original.acceptedAt ? formatDate(row.original.acceptedAt) : "-"}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
      meta: {
        excelHeader: "RFQ 승인일",
      },
    },
  ]

  return [selectColumn, ...dataColumns]
} 