"use client"

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

import { exportTableToExcel } from "@/lib/export"
import { Button } from "@/components/ui/button"
import { Project } from "@/db/schema"
import { CoverTemplateDialog } from "./cover-template-dialog"

interface ItemsTableToolbarActionsProps {
  table: Table<Project>
}

export function ProjectTableToolbarActions({ table }: ItemsTableToolbarActionsProps) {
  const [templateDialogOpen, setTemplateDialogOpen] = React.useState(false)
  const [selectedProject, setSelectedProject] = React.useState<Project | null>(null)

  const handleTemplateClick = () => {
    const selectedRows = table.getFilteredSelectedRowModel().rows
    if (selectedRows.length !== 1) {
      toast.error("프로젝트를 하나만 선택해주세요")
      return
    }
    setSelectedProject(selectedRows[0].original)
    setTemplateDialogOpen(true)
  }

  return (
    <div className="flex items-center gap-2">
      <Button
        variant="outline"
        size="sm"
        onClick={handleTemplateClick}
        disabled={table.getFilteredSelectedRowModel().rows.length !== 1}
      >
        <FileText className="mr-2 h-4 w-4" />
        커버 페이지 템플릿
      </Button>

      <CoverTemplateDialog
        open={templateDialogOpen}
        onOpenChange={setTemplateDialogOpen}
        project={selectedProject}
      />
    </div>
  )
}