"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"
import { useTransition } from "react"
import { Button } from "@/components/ui/button"
import { Plus, Send, Mail, CheckSquare } from "lucide-react"
import { BiddingCompany } from "./bidding-pre-quote-vendor-columns"
import { BiddingPreQuoteVendorCreateDialog } from "./bidding-pre-quote-vendor-create-dialog"
import { BiddingPreQuoteInvitationDialog } from "./bidding-pre-quote-invitation-dialog"
import { BiddingPreQuoteSelectionDialog } from "./bidding-pre-quote-selection-dialog"
import { Bidding } from "@/db/schema" 
import { useToast } from "@/hooks/use-toast"

interface BiddingPreQuoteVendorToolbarActionsProps {
  table: Table<BiddingCompany>
  biddingId: number
  bidding: Bidding
  biddingCompanies: BiddingCompany[]
  onOpenItemsDialog: () => void
  onOpenTargetPriceDialog: () => void
  onOpenSelectionReasonDialog: () => void
  onSuccess: () => void
}

export function BiddingPreQuoteVendorToolbarActions({
  table,
  biddingId,
  bidding,
  biddingCompanies,
  onOpenItemsDialog,
  onOpenTargetPriceDialog,
  onOpenSelectionReasonDialog,
  onSuccess
}: BiddingPreQuoteVendorToolbarActionsProps) {
  const { toast } = useToast()
  const [isPending, startTransition] = useTransition()
  const [isCreateDialogOpen, setIsCreateDialogOpen] = React.useState(false)
  const [isInvitationDialogOpen, setIsInvitationDialogOpen] = React.useState(false)
  const [isSelectionDialogOpen, setIsSelectionDialogOpen] = React.useState(false)

  const handleCreateCompany = () => {
    setIsCreateDialogOpen(true)
  }

  const handleSendInvitations = () => {
    setIsInvitationDialogOpen(true)
  }

  const handleManageSelection = () => {
    const selectedRows = table.getFilteredSelectedRowModel().rows
    if (selectedRows.length === 0) {
      toast({
        title: '선택 필요',
        description: '본입찰 선정 상태를 변경할 업체를 선택해주세요.',
        variant: 'destructive',
      })
      return
    }
    setIsSelectionDialogOpen(true)
  }



  return (
    <>
      <div className="flex items-center gap-2">
        <Button
          variant="outline"
          size="sm"
          onClick={handleCreateCompany}
          disabled={isPending}
        >
          <Plus className="mr-2 h-4 w-4" />
          업체 추가
        </Button>
        
        <Button
          variant="default"
          size="sm"
          onClick={handleSendInvitations}
          disabled={isPending}
        >
          <Mail className="mr-2 h-4 w-4" />
          초대 발송
        </Button>
        
        <Button
          variant="secondary"
          size="sm"
          onClick={handleManageSelection}
          disabled={isPending}
        >
          <CheckSquare className="mr-2 h-4 w-4" />
          본입찰 선정
        </Button>
      </div>

      <BiddingPreQuoteVendorCreateDialog
        biddingId={biddingId}
        open={isCreateDialogOpen}
        onOpenChange={setIsCreateDialogOpen}
        onSuccess={() => {
          onSuccess()
          setIsCreateDialogOpen(false)
        }}
      />

      <BiddingPreQuoteInvitationDialog
        open={isInvitationDialogOpen}
        onOpenChange={setIsInvitationDialogOpen}
        companies={biddingCompanies}
        biddingId={biddingId}
        biddingTitle={bidding.title}
        projectName={bidding.projectName}
        onSuccess={onSuccess}
      />

      <BiddingPreQuoteSelectionDialog
        open={isSelectionDialogOpen}
        onOpenChange={setIsSelectionDialogOpen}
        selectedCompanies={table.getFilteredSelectedRowModel().rows.map(row => row.original)}
        onSuccess={() => {
          onSuccess()
          table.resetRowSelection()
        }}
      />
    </>
  )
}
