"use client";

import { ColumnDef } from "@tanstack/react-table";
import { DetailDwgReceiptItem } from "../actions";
import { formatDolceDateTime } from "../utils/date-formatter";

// DOLCE API ENM 필드가 제대로 번역되지 않아 직접 매핑
const DRAWING_USAGE_MAP: Record<string, { ko: string; en: string }> = {
  APP: { ko: "승인용", en: "Approval" },
  WOR: { ko: "작업용", en: "Working" },
  REC: { ko: "입수용", en: "GTT→SHI" },
  SUB: { ko: "제출용", en: "SHI→GTT" },
};

const REGISTER_KIND_MAP: Record<string, { ko: string; en: string }> = {
  APPR: { ko: "승인 제출용 도면(Full)", en: "For Approval(Full)" },
  APPP: { ko: "승인 제출용 도면(Partial)", en: "For Approval(Partial)" },
  WORK: { ko: "작업용 입수도면(Full)", en: "For Working(Full)" },
  WORP: { ko: "작업용 입수도면(Partial)", en: "For Working(Partial)" },
  RECW: { ko: "Working 도면입수(GTT→SHI)", en: "Working Dwg(GTT→SHI)" },
  RECP: { ko: "Pre. 도면입수(GTT→SHI)", en: "Pre. Dwg(GTT→SHI)" },
};

// 카테고리는 API에서 ENM이 제공되는 것으로 가정 (필요시 추가)
const translateDrawingUsage = (code: string | null, lng: string): string => {
  if (!code) return "";
  const mapped = DRAWING_USAGE_MAP[code];
  if (!mapped) return code;
  return lng === "en" ? mapped.en : mapped.ko;
};

const translateRegisterKind = (code: string | null, lng: string): string => {
  if (!code) return "";
  const mapped = REGISTER_KIND_MAP[code];
  if (!mapped) return code;
  return lng === "en" ? mapped.en : mapped.ko;
};

// 다국어 지원 컬럼 생성 함수
export function createDetailDrawingColumns(
  lng: string,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  t: any
): ColumnDef<DetailDwgReceiptItem>[] {
  return [
    {
      accessorKey: "RegisterSerialNo",
      header: t("detailDrawing.columns.serialNo"),
      minSize: 80,
      cell: ({ row }) => {
        return <div className="text-center">{row.getValue("RegisterSerialNo")}</div>;
      },
    },
    {
      accessorKey: "DrawingRevNo",
      header: t("detailDrawing.columns.revNo"),
      minSize: 100,
      cell: ({ row }) => {
        return <div className="font-medium">{row.getValue("DrawingRevNo")}</div>;
      },
    },
    {
      accessorKey: "Status",
      header: t("detailDrawing.columns.status"),
      minSize: 120,
      cell: ({ row }) => {
        return <div className="text-center">{row.getValue("Status")}</div>;
      },
    },
    {
      accessorKey: "CategoryENM",
      header: t("detailDrawing.columns.category"),
      minSize: 120,
      cell: ({ row }) => {
        const categoryENM = row.getValue("CategoryENM") as string;
        const categoryNM = row.original.CategoryNM;
        // 영어인 경우 ENM, 한국어인 경우 NM 사용
        return <div>{lng === "en" ? (categoryENM || categoryNM) : (categoryNM || categoryENM)}</div>;
      },
    },
    {
      accessorKey: "DrawingUsage",
      header: t("detailDrawing.columns.drawingUsage"),
      minSize: 100,
      cell: ({ row }) => {
        // API 응답의 DrawingUsage는 코드값이므로 직접 매핑하여 번역
        const usageCode = row.getValue("DrawingUsage") as string;
        const translated = translateDrawingUsage(usageCode, lng);
        return <div>{translated}</div>;
      },
    },
    {
      accessorKey: "RegisterKind",
      header: t("detailDrawing.columns.registerKind"),
      minSize: 180,
      cell: ({ row }) => {
        // API 응답의 RegisterKind는 코드값이므로 직접 매핑하여 번역
        const kindCode = row.getValue("RegisterKind") as string;
        const translated = translateRegisterKind(kindCode, lng);
        return <div>{translated}</div>;
      },
    },
    {
      accessorKey: "CreateUserNM",
      header: t("detailDrawing.columns.createdBy"),
      minSize: 150,
      cell: ({ row }) => {
        const userENM = row.original.CreateUserENM;
        const userNM = row.getValue("CreateUserNM") as string;
        return <div>{lng === "en" ? (userENM || userNM) : (userNM || userENM)}</div>;
      },
    },
    {
      accessorKey: "CreateDt",
      header: t("detailDrawing.columns.createdAt"),
      minSize: 200,
      cell: ({ row }) => {
        const date = row.getValue("CreateDt") as string;
        return <div className="text-sm text-muted-foreground">{formatDolceDateTime(date)}</div>;
      },
    },
  ];
}
