"use client"

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

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

import { AddCandidateDialog } from "./add-candidates-dialog"
import { VendorCandidates } from "@/db/schema/vendors"
import { DeleteCandidatesDialog } from "./delete-candidates-dialog"
import { InviteCandidatesDialog } from "./invite-candidates-dialog"
import { ImportVendorCandidatesButton } from "./import-button"
import { exportVendorCandidateTemplate } from "./excel-template-download"


interface CandidatesTableToolbarActionsProps {
  table: Table<VendorCandidates>
}

export function CandidatesTableToolbarActions({ table }: CandidatesTableToolbarActionsProps) {
  const selectedRows = table.getFilteredSelectedRowModel().rows
  const hasSelection = selectedRows.length > 0
  const [refreshKey, setRefreshKey] = React.useState(0)

  // Handler to refresh the table after import
  const handleImportSuccess = () => {
    // Trigger a refresh of the table data
    setRefreshKey(prev => prev + 1)
  }

  return (
    <div className="flex items-center gap-2">
      {/* Show actions only when rows are selected */}
      {hasSelection ? (
        <>
          {/* Invite dialog - new addition */}
          <InviteCandidatesDialog
            candidates={selectedRows.map((row) => row.original)}
            onSuccess={() => table.toggleAllRowsSelected(false)}
          />

          {/* Delete dialog */}
          <DeleteCandidatesDialog
            candidates={selectedRows.map((row) => row.original)}
            onSuccess={() => table.toggleAllRowsSelected(false)}
          />
        </>
      ) : null}

      {/* Add new candidate dialog */}
      <AddCandidateDialog />
      
      {/* Import Excel button */}
      <ImportVendorCandidatesButton onSuccess={handleImportSuccess} />
      
      {/* Export dropdown menu */}
      <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: "vendor-candidates",
                excludeColumns: ["select", "actions"],
                useGroupHeader: false,
              })
            }}
          >
            <FileDown className="mr-2 h-4 w-4" />
            <span>Export Current Data</span>
          </DropdownMenuItem>
          <DropdownMenuItem onClick={exportVendorCandidateTemplate}>
            <FileDown className="mr-2 h-4 w-4" />
            <span>Download Template</span>
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenu>
    </div>
  )
}