"use client"

import * as React from "react"
import { type DataTableRowAction } from "@/types/table"
import { type ColumnDef } from "@tanstack/react-table"
import { Download, Eye, FileText, FilePlus } from "lucide-react"
import { Checkbox } from "@/components/ui/checkbox";

import { formatDate } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { quickDownload } from "@/lib/file-download"

import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { Project } from "@/db/schema"

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<Project> | null>>
  onDetailClick: (project: Project) => void
  onTemplateManage: (project: Project) => void
}

export function getColumns({ setRowAction, onDetailClick, onTemplateManage }: GetColumnsProps): ColumnDef<Project>[] {
  return [
      // Checkbox 
      {
        id: "select",
        header: ({ table }) => (
          <Checkbox
            checked={table.getIsAllPageRowsSelected() || (table.getIsSomePageRowsSelected() && "indeterminate")}
            onCheckedChange={(v) => table.toggleAllPageRowsSelected(!!v)}
            aria-label="select all"
            className="translate-y-0.5"
          />
        ),
        cell: ({ row }) => (
          <Checkbox
            checked={row.getIsSelected()}
            onCheckedChange={(v) => row.toggleSelected(!!v)}
            aria-label="select row"
            className="translate-y-0.5"
          />
        ),
        size: 40,
        enableSorting: false,
        enableHiding: false,
      },
    {
      accessorKey: "code",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트 코드" />
      ),
      meta: {
        excelHeader: "Project Code",
      },
    },
    {
      accessorKey: "name",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트명" />
      ),
      meta: {
        excelHeader: "Project Name",
      },
    },
    {
      id: "coverTemplate",
      enableResizing: true,
      header: "커버 템플릿",
      cell: ({ row }) => {
        const project = row.original
        const hasTemplate = !!project.coverTemplatePath
        
        return (
          <div className="flex items-center gap-2">
            {hasTemplate ? (
              <>
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => quickDownload(project.coverTemplatePath!, `${project.code}_template.docx`)}
                  title="템플릿 다운로드"
                >
                  <FileText className="h-4 w-4 text-blue-600" />
                </Button>
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => onTemplateManage(project)}
                  title="템플릿 관리"
                >
                  <Eye className="h-4 w-4" />
                </Button>
              </>
            ) : (
              <Button
                variant="outline"
                size="sm"
                onClick={() => onTemplateManage(project)}
              >
                <FilePlus className="h-4 w-4 mr-1" />
                생성
              </Button>
            )}
          </div>
        )
      },
    },
    {
      id: "generatedCover",
      enableResizing: true,
      header: "생성된 커버",
      cell: ({ row }) => {
        const project = row.original
        const hasGenerated = !!project.generatedCoverPath
        const generatedAt = project.generatedCover?.generatedAt
        
        return (
          <div className="flex flex-col gap-1">
            {hasGenerated ? (
              <>
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => quickDownload(
                    project.generatedCoverPath!, 
                    project.generatedCover?.fileName || `${project.code}_cover.docx`
                  )}
                  className="justify-start"
                >
                  <Download className="h-4 w-4 mr-2 text-green-600" />
                  <span className="text-xs">다운로드</span>
                </Button>
                {generatedAt && (
                  <span className="text-xs text-muted-foreground pl-2">
                    {formatDate(new Date(generatedAt), "KR")}
                  </span>
                )}
              </>
            ) : (
              <span className="text-xs text-muted-foreground px-2">-</span>
            )}
          </div>
        )
      },
    },
    {
      accessorKey: "OWN_NM",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="선주명" />
      ),
      meta: {
        excelHeader: "Owner Name",
      },
    },
    {
      accessorKey: "createdAt",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="생성일" />
      ),
      meta: {
        excelHeader: "Created At",
      },
      cell: ({ cell }) => {
        const dateVal = cell.getValue() as Date
        return formatDate(dateVal, "KR")
      },
    },
    {
      accessorKey: "updatedAt",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="수정일" />
      ),
      meta: {
        excelHeader: "Updated At",
      },
      cell: ({ cell }) => {
        const dateVal = cell.getValue() as Date
        return formatDate(dateVal, "KR")
      },
    },
  ]
}