"use client";

import { ColumnDef } from "@tanstack/react-table";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Download } from "lucide-react";
import type { DocumentListItem } from "@/lib/swp/document-service";

export const swpDocumentColumns: ColumnDef<DocumentListItem>[] = [
  {
    accessorKey: "LTST_ACTV_STAT",
    header: "상태",
    cell: ({ row }) => {
      const status = row.original.LTST_ACTV_STAT;
      if (!status) return "-";

      const color =
        status.includes("Complete") ? "bg-green-100 text-green-800" :
        status.includes("Progress") ? "bg-blue-100 text-blue-800" :
        status.includes("Pending") || status.includes("Ready") ? "bg-yellow-100 text-yellow-800" :
        "bg-gray-100 text-gray-800";

      return (
        <Badge variant="outline" className={color}>
          {status}
        </Badge>
      );
    },
    size: 120,
  },
  {
    accessorKey: "DOC_NO",
    header: "문서번호",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.original.DOC_NO}</div>
    ),
    size: 250,
  },
  {
    accessorKey: "DOC_TITLE",
    header: "문서제목",
    cell: ({ row }) => (
      <div className="max-w-md truncate" title={row.original.DOC_TITLE}>
        {row.original.DOC_TITLE}
      </div>
    ),
    size: 300,
  },
  {
    accessorKey: "PROJ_NO",
    header: "프로젝트",
    cell: ({ row }) => (
      <div>
        <div className="font-medium">{row.original.PROJ_NO}</div>
        {row.original.PROJ_NM && (
          <div className="text-xs text-muted-foreground max-w-[150px] truncate">
            {row.original.PROJ_NM}
          </div>
        )}
      </div>
    ),
    size: 150,
  },
  {
    accessorKey: "PKG_NO",
    header: "패키지",
    cell: ({ row }) => row.original.PKG_NO || "-",
    size: 100,
  },
  {
    accessorKey: "VNDR_CD",
    header: "업체",
    cell: ({ row }) => (
      <div>
        {row.original.VNDR_CD && (
          <div className="text-xs text-muted-foreground">{row.original.VNDR_CD}</div>
        )}
        {row.original.CPY_NM && (
          <div className="text-sm truncate max-w-[120px]" title={row.original.CPY_NM}>
            {row.original.CPY_NM}
          </div>
        )}
      </div>
    ),
    size: 120,
  },
  {
    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: "LTST_REV_NO",
    header: "최신 REV",
    cell: ({ row }) => row.original.LTST_REV_NO || "-",
    size: 80,
  },
  {
    id: "stats",
    header: "파일",
    cell: ({ row }) => (
      <div className="text-center">
        <div className="text-sm font-medium">
          {row.original.fileCount}개
        </div>
        {row.original.standbyFileCount > 0 && (
          <div className="text-xs text-yellow-600">
            대기중 {row.original.standbyFileCount}
          </div>
        )}
      </div>
    ),
    size: 100,
  },
  {
    id: "actions",
    header: "액션",
    cell: ({ row }) => (
      <Button
        variant="ghost"
        size="sm"
        onClick={(e) => {
          e.stopPropagation(); // 행 클릭 이벤트 방지
          // 커버페이지 다운로드 핸들러는 부모 컴포넌트에서 제공
          const event = new CustomEvent('coverDownload', {
            detail: { document: row.original }
          });
          window.dispatchEvent(event);
        }}
        className="h-8 px-2"
      >
        <Download className="h-4 w-4 mr-1" />
        커버페이지
      </Button>
    ),
    size: 120,
  },
];
