"use client";

import React from "react";
import { ColumnDef } from "@tanstack/react-table";
import { Badge } from "@/components/ui/badge";
import type { SwpFileApiResponse } from "@/lib/swp/api-client";

export const swpInboxDocumentColumns: ColumnDef<SwpFileApiResponse>[] = [
  {
    accessorKey: "STAT_NM",
    header: "최신 리비전의 최신 파일 상태",
    cell: ({ row }) => {
      const statNm = row.original.STAT_NM;
      const stat = row.original.STAT;
      const displayStatus = statNm || stat || "-";
      
      if (!stat) return displayStatus;

      // STAT 코드 기반 색상 결정
      const color =
        stat === "SCW03" || stat === "SCW08" ? "bg-green-100 text-green-800" :      // Complete, Checked
        stat === "SCW02" ? "bg-blue-100 text-blue-800" :                            // Processing
        stat === "SCW01" ? "bg-yellow-100 text-yellow-800" :                        // Standby
        stat === "SCW04" || stat === "SCW05" || stat === "SCW06" ? "bg-red-100 text-red-800" : // Reject, Error Zip, Error Meta
        stat === "SCW07" ? "bg-purple-100 text-purple-800" :                        // Send for Eng Verification
        stat === "SCW09" ? "bg-gray-100 text-gray-800" :                            // Cancelled
        stat === "SCW00" ? "bg-orange-100 text-orange-800" :                        // Upload
        "bg-gray-100 text-gray-800";                                                 // 기타

      return (
        <Badge variant="outline" className={color}>
          {displayStatus}
        </Badge>
      );
    },
    size: 120,
  },
  {
    accessorKey: "OWN_DOC_NO",
    header: "OWN_DOC_NO",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.original.OWN_DOC_NO || "-"}</div>
    ),
    size: 250,
  },
  {
    accessorKey: "FILE_NM",
    header: "파일명",
    cell: ({ row }) => (
      <div className="max-w-md truncate" title={row.original.FILE_NM}>
        {row.original.FILE_NM}
      </div>
    ),
    size: 300,
  },
  {
    accessorKey: "STAGE",
    header: "스테이지",
    cell: ({ row }) => {
      const stage = row.original.STAGE;
      if (!stage) return "-";

      const color =
        stage === "IFC" ? "bg-green-100 text-green-800" :
        stage === "IFA" ? "bg-blue-100 text-blue-800" :
        "bg-gray-100 text-gray-800";

      return (
        <Badge variant="outline" className={color}>
          {stage}
        </Badge>
      );
    },
    size: 80,
  },
  {
    accessorKey: "REV_NO",
    header: "REV",
    cell: ({ row }) => row.original.REV_NO || "-",
    size: 80,
  },
  {
    accessorKey: "ACTV_NO",
    header: "Activity",
    cell: ({ row }) => {
      const actvNo = row.original.ACTV_NO;
      if (!actvNo) return <span className="text-muted-foreground">-</span>;
      return <div className="font-mono text-xs">{actvNo}</div>;
    },
    size: 120,
  },
  {
    accessorKey: "FILE_SZ",
    header: "파일 크기",
    cell: ({ row }) => {
      const size = row.original.FILE_SZ;
      if (!size) return "-";
      const bytes = parseInt(size);
      if (isNaN(bytes)) return size;
      
      if (bytes < 1024) return `${bytes} B`;
      if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
      if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
      return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
    },
    size: 100,
  },
  {
    accessorKey: "CRTE_DTM",
    header: "생성일시",
    cell: ({ row }) => {
      const date = row.original.CRTE_DTM;
      if (!date) return "-";
      try {
        return new Date(date).toLocaleString("ko-KR", {
          year: "numeric",
          month: "2-digit",
          day: "2-digit",
          hour: "2-digit",
          minute: "2-digit",
        });
      } catch {
        return date;
      }
    },
    size: 150,
  },
];

