"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"
// import { Download, FileDown } from "lucide-react"
// import { toast } from "sonner"

// import { exportTableToExcel } from "@/lib/export"
// import { Button } from "@/components/ui/button"
// import {
//   DropdownMenu,
//   DropdownMenuContent,
//   DropdownMenuItem,
//   DropdownMenuTrigger,
// } from "@/components/ui/dropdown-menu"

import { DeletePqsDialog } from "./delete-pqs-dialog"
import { AddPqDialog } from "./add-pq-dialog"
import { PqCriterias } from "@/db/schema/pq"
import { toast } from "sonner"
// import { ImportPqButton } from "./import-pq-button"
// import { exportPqTemplate } from "./pq-excel-template"

interface PqTableToolbarActionsProps {
  table: Table<PqCriterias>
  pqListId: number
  pqListInfo: Awaited<ReturnType<typeof import("../service").getPQListInfo>>
}

export function PqTableToolbarActions({ 
  table, 
  pqListId,
  pqListInfo
}: PqTableToolbarActionsProps) {
  // PQ 리스트가 ACTIVE 상태인지 확인
  const isActive = pqListInfo.success && pqListInfo.data.status === "ACTIVE";
  
  // ACTIVE 상태일 때 기능 제한
  const handleRestrictedAction = () => {
    toast.error("활성화된 PQ 목록은 수정할 수 없습니다. 먼저 PQ 목록을 비활성화해주세요.");
  };
  
  return (
    <div className="flex items-center gap-2">
      {table.getFilteredSelectedRowModel().rows.length > 0 ? (
        isActive ? (
          <button
            onClick={handleRestrictedAction}
            className="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 border border-input bg-background hover:bg-accent hover:text-accent-foreground h-9 px-3 text-destructive hover:text-destructive"
          >
            PQ 항목 삭제
          </button>
        ) : (
          <DeletePqsDialog
            pqs={table
              .getFilteredSelectedRowModel()
              .rows.map((row) => row.original)}
            onSuccess={() => table.toggleAllRowsSelected(false)}
          />
        )
      ) : null}
      
      {isActive ? (
        <button
          onClick={handleRestrictedAction}
          className="inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground hover:bg-primary/90 h-9 px-3"
        >
          PQ 항목 추가
        </button>
      ) : (
        <AddPqDialog pqListId={pqListId} />
      )}
      
      {/* Import 버튼 */}
      {/* <ImportPqButton 
        pqListId={pqListId} 
        onSuccess={handleImportSuccess} 
      /> */}
      
      {/* Export 드롭다운 메뉴 */}
      {/* <DropdownMenu>
        <DropdownMenuTrigger asChild>
          <Button variant="outline" size="sm" className="gap-2">
            <Download className="size-4" aria-hidden="true" />
            <span className="hidden sm:inline">Export</span>
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent align="end">
          <DropdownMenuItem
            onClick={() =>
              exportTableToExcel(table, {
                filename: `pq-list-${pqListId}-criteria`,
                excludeColumns: ["select", "actions"],
              })
            }
          >
            <FileDown className="mr-2 h-4 w-4" />
            <span>현재 데이터 내보내기</span>
          </DropdownMenuItem>
          <DropdownMenuItem onClick={() => exportPqTemplate()}>
            <FileDown className="mr-2 h-4 w-4" />
            <span>템플릿 다운로드</span>
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenu> */}
    </div>
  )
}