"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"

import { AddAttachmentDialog } from "./add-attachment-dialog"
import { ConfirmDocumentsDialog } from "./confirm-documents-dialog"
import { TbeRequestDialog } from "./tbe-request-dialog"
import { DeleteAttachmentsDialog } from "./delete-attachment-dialog"

interface RfqAttachmentsTableToolbarActionsProps {
  table: Table<any>
  rfqId: number
}

export function RfqAttachmentsTableToolbarActions({ 
  table, 
  rfqId 
}: RfqAttachmentsTableToolbarActionsProps) {
  
  // 선택된 행들 가져오기
  const selectedRows = table.getFilteredSelectedRowModel().rows
  const selectedAttachments = selectedRows.map((row) => row.original)
  const selectedCount = selectedRows.length

  return (
    <div className="flex items-center gap-2">
      {/** 선택된 로우가 있으면 삭제 다이얼로그 */}
      {selectedCount > 0 && (
        <DeleteAttachmentsDialog
          attachments={selectedAttachments}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}

      {/** 새 첨부 추가 다이얼로그 */}
      <AddAttachmentDialog rfqId={rfqId} />

      {/** 문서 확정 다이얼로그 */}
      <ConfirmDocumentsDialog 
        rfqId={rfqId}
        onSuccess={() => {
          // 성공 후 필요한 작업 (예: 페이지 새로고침)
          window.location.reload()
        }}
      />

      {/** TBE 요청 다이얼로그 (선택된 행이 있을 때만 활성화) */}
      <TbeRequestDialog
        rfqId={rfqId}
        attachments={selectedAttachments}
        onSuccess={() => {
          // 선택 해제 및 페이지 새로고침
          table.toggleAllRowsSelected(false)
          window.location.reload()
        }}
      />
    </div>
  )
}