"use client";

import { ColumnDef } from "@tanstack/react-table";
import { GttDwgReceiptItem } from "../actions";
import { translateDrawingMoveGbn } from "../utils/code-translator";
import { formatDolceDateYYYYMMDD, formatDolceDateTime } from "../utils/date-formatter";

// Document Type 필터
export type DocumentType = "ALL" | "GTT_DELIVERABLES" | "SHI_INPUT";

interface GttDrawingListColumnsOptions {
  documentType: DocumentType;
  lng: string;
  t: any;
}

export function createGttDrawingListColumns({
  documentType,
  lng,
  t,
}: GttDrawingListColumnsOptions): ColumnDef<GttDwgReceiptItem>[] {
  const baseColumns: ColumnDef<GttDwgReceiptItem>[] = [
    {
      accessorKey: "DrawingNo",
      header: t("drawingList.columns.drawingNo"),
      minSize: 200,
      cell: ({ row }) => {
        return <div className="font-medium">{row.getValue("DrawingNo")}</div>;
      },
    },
    {
      accessorKey: "DrawingName",
      header: t("drawingList.columns.drawingName"),
      minSize: 400,
      cell: ({ row }) => {
        return <div>{row.getValue("DrawingName")}</div>;
      },
    },
    {
      accessorKey: "Discipline",
      header: t("drawingList.columns.discipline"),
      minSize: 80,
    },
    {
      accessorKey: "Manager",
      header: t("drawingList.columns.manager"),
      minSize: 200,
      cell: ({ row }) => {
        const managerENM = row.original.ManagerENM;
        const manager = row.getValue("Manager");
        return <div>{managerENM || manager}</div>;
      },
    },
    {
      accessorKey: "DrawingMoveGbn",
      header: t("drawingList.columns.category"),
      minSize: 120,
      cell: ({ row }) => {
        const value = row.getValue("DrawingMoveGbn") as string;
        return <div>{translateDrawingMoveGbn(value, lng)}</div>;
      },
    },
  ];

  // Document Type에 따른 날짜 컬럼
  const dateColumns: ColumnDef<GttDwgReceiptItem>[] = [];

  // ALL: 모든 컬럼 표시
  if (documentType === "ALL") {
    dateColumns.push(
      {
        accessorKey: "GTTInput_PlanDate",
        header: t("drawingList.columns.gttInputPlanDate"),
        minSize: 150,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTInput_PlanDate")),
      },
      {
        accessorKey: "GTTInput_ResultDate",
        header: t("drawingList.columns.gttInputResultDate"),
        minSize: 150,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTInput_ResultDate")),
      },
      {
        accessorKey: "GTTPreDwg_PlanDate",
        header: t("drawingList.columns.gttPreDwgPlanDate"),
        minSize: 140,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTPreDwg_PlanDate")),
      },
      {
        accessorKey: "GTTPreDwg_ResultDate",
        header: t("drawingList.columns.gttPreDwgResultDate"),
        minSize: 140,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTPreDwg_ResultDate")),
      },
      {
        accessorKey: "GTTWorkingDwg_PlanDate",
        header: t("drawingList.columns.gttWorkingDwgPlanDate"),
        minSize: 160,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTWorkingDwg_PlanDate")),
      },
      {
        accessorKey: "GTTWorkingDwg_ResultDate",
        header: t("drawingList.columns.gttWorkingDwgResultDate"),
        minSize: 160,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTWorkingDwg_ResultDate")),
      }
    );
  }
  // SHI_INPUT: 도면제출 (GTTInput만)
  else if (documentType === "SHI_INPUT") {
    dateColumns.push(
      {
        accessorKey: "GTTInput_PlanDate",
        header: t("drawingList.columns.inputPlanDate"),
        minSize: 120,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTInput_PlanDate")),
      },
      {
        accessorKey: "GTTInput_ResultDate",
        header: t("drawingList.columns.inputResultDate"),
        minSize: 120,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTInput_ResultDate")),
      }
    );
  }
  // GTT_DELIVERABLES: 도면입수 (Pre, Working)
  else if (documentType === "GTT_DELIVERABLES") {
    dateColumns.push(
      {
        accessorKey: "GTTPreDwg_PlanDate",
        header: t("drawingList.columns.prePlanDate"),
        minSize: 120,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTPreDwg_PlanDate")),
      },
      {
        accessorKey: "GTTPreDwg_ResultDate",
        header: t("drawingList.columns.preResultDate"),
        minSize: 120,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTPreDwg_ResultDate")),
      },
      {
        accessorKey: "GTTWorkingDwg_PlanDate",
        header: t("drawingList.columns.workingPlanDate"),
        minSize: 130,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTWorkingDwg_PlanDate")),
      },
      {
        accessorKey: "GTTWorkingDwg_ResultDate",
        header: t("drawingList.columns.workingResultDate"),
        minSize: 130,
        cell: ({ row }) => formatDolceDateYYYYMMDD(row.getValue("GTTWorkingDwg_ResultDate")),
      }
    );
  }

  // 생성일시 컬럼
  const endColumns: ColumnDef<GttDwgReceiptItem>[] = [
    {
      accessorKey: "CreateDt",
      header: t("drawingList.columns.createDt"),
      minSize: 200,
      cell: ({ row }) => {
        const date = row.getValue("CreateDt") as string;
        return <div className="text-sm text-muted-foreground">{formatDolceDateTime(date)}</div>;
      },
    },
  ];

  return [...baseColumns, ...dateColumns, ...endColumns];
}

