"use client"

import * as React from "react"
import { useRouter } from "next/navigation"
import { useTransition } from "react"
import { Button } from "@/components/ui/button"
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { Plus, Send, RotateCcw, XCircle, Trophy, FileText, DollarSign, RotateCw } from "lucide-react"
import { registerBidding, markAsDisposal, createRebidding, cancelAwardRatio } from "@/lib/bidding/detail/service"
import { sendBiddingBasicContracts, getSelectedVendorsForBidding } from "@/lib/bidding/pre-quote/service"
import { increaseRoundOrRebid } from "@/lib/bidding/service"

import { BiddingDetailVendorCreateDialog } from "../../../../components/bidding/manage/bidding-detail-vendor-create-dialog"
import { BiddingDocumentUploadDialog } from "./bidding-document-upload-dialog"
import { Bidding } from "@/db/schema"
import { useToast } from "@/hooks/use-toast"
import { QuotationVendor } from "@/lib/bidding/detail/service"

interface BiddingDetailVendorToolbarActionsProps {
  biddingId: number
  bidding: Bidding
  userId: string
  onOpenAwardDialog: () => void
  onSuccess: () => void
  winnerVendor?: QuotationVendor | null // 100% 낙찰된 벤더
}

export function BiddingDetailVendorToolbarActions({
  biddingId,
  bidding,
  userId,
  onOpenAwardDialog,
  onSuccess,
  winnerVendor
}: BiddingDetailVendorToolbarActionsProps) {
  const router = useRouter()
  const { toast } = useToast()
  const [isPending, startTransition] = useTransition()
  const [isCreateDialogOpen, setIsCreateDialogOpen] = React.useState(false)
  const [isDocumentDialogOpen, setIsDocumentDialogOpen] = React.useState(false)
  const [isPricesDialogOpen, setIsPricesDialogOpen] = React.useState(false)
  const [isBiddingInvitationDialogOpen, setIsBiddingInvitationDialogOpen] = React.useState(false)
  const [selectedVendors, setSelectedVendors] = React.useState<any[]>([])
  const [isRoundIncreaseDialogOpen, setIsRoundIncreaseDialogOpen] = React.useState(false)
  const [isCancelAwardDialogOpen, setIsCancelAwardDialogOpen] = React.useState(false)

  // 본입찰 초대 다이얼로그가 열릴 때 선정된 업체들 조회
  React.useEffect(() => {
    if (isBiddingInvitationDialogOpen) {
      getSelectedVendors().then(vendors => {
        setSelectedVendors(vendors)
      })
    }
  }, [isBiddingInvitationDialogOpen, biddingId])

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

  const handleDocumentUpload = () => {
    setIsDocumentDialogOpen(true)
  }

  const handleViewVendorPrices = () => {
    setIsPricesDialogOpen(true)
  }

  const handleRegister = () => {
    if (!bidding.targetPrice) {
      toast({
        title: '오류',
        description: '내정가가 산정되어야 입찰 초대를 할 수 있습니다.',
        variant: 'destructive',
      })
      return
    }
    // 본입찰 초대 다이얼로그 열기
    setIsBiddingInvitationDialogOpen(true)
  }

  // const handleBiddingInvitationSend = async (data: any) => {
  //   try {
  //     // 1. 기본계약 발송
  //     const contractResult = await sendBiddingBasicContracts(
  //       biddingId,
  //       data.vendors,
  //       data.generatedPdfs,
  //       data.message
  //     )

  //     if (!contractResult.success) {
  //       toast({
  //         title: '기본계약 발송 실패',
  //         description: contractResult.error,
  //         variant: 'destructive',
  //       })
  //       return
  //     }

  //     // 2. 입찰 등록 진행
  //     const registerResult = await registerBidding(bidding.id, userId)

  //     if (registerResult.success) {
  //       toast({
  //         title: '본입찰 초대 완료',
  //         description: '기본계약 발송 및 본입찰 초대가 완료되었습니다.',
  //       })
  //       setIsBiddingInvitationDialogOpen(false)
  //       router.refresh()
  //       onSuccess()
  //     } else {
  //       toast({
  //         title: '오류',
  //         description: registerResult.error,
  //         variant: 'destructive',
  //       })
  //     }
  //   } catch (error) {
  //     console.error('본입찰 초대 실패:', error)
  //     toast({
  //       title: '오류',
  //       description: '본입찰 초대에 실패했습니다.',
  //       variant: 'destructive',
  //     })
  //   }
  // }

  // 선정된 업체들 조회 (서버 액션 함수 사용)
  const getSelectedVendors = async () => {
    try {
      const result = await getSelectedVendorsForBidding(biddingId)
      if (result.success) {
        return result.vendors
      } else {
        console.error('선정된 업체 조회 실패:', result.error)
        return []
      }
    } catch (error) {
      console.error('선정된 업체 조회 실패:', error)
      return []
    }
  }

  const handleMarkAsDisposal = () => {
    startTransition(async () => {
      const result = await markAsDisposal(bidding.id, userId)

      if (result.success) {
        toast({
          title: result.message,
          description: result.message,
        })
        router.refresh()
      } else {
        toast({
          title: result.error,
          description: result.error,
          variant: 'destructive',
        })
      }
    })
  }

  const handleRoundIncrease = () => {
    startTransition(async () => {
      const result = await increaseRoundOrRebid(bidding.id, userId, 'round_increase')

      if (result.success) {
        toast({
          title: "성공",
          description: result.message,
        })
        router.push(`/evcp/bid`)
        onSuccess()
      } else {
        toast({
          title: "오류",
          description: result.error || "차수증가 중 오류가 발생했습니다.",
          variant: 'destructive',
        })
      }
    })
  }

  const handleCancelAward = () => {
    if (!winnerVendor) return

    startTransition(async () => {
      const result = await cancelAwardRatio(winnerVendor.id)

      if (result.success) {
        toast({
          title: "성공",
          description: result.message,
        })
        setIsCancelAwardDialogOpen(false)
        onSuccess()
      } else {
        toast({
          title: "오류",
          description: result.error || "발주비율 취소 중 오류가 발생했습니다.",
          variant: 'destructive',
        })
      }
    })
  }

  const handleRoundIncreaseWithNavigation = () => {
    startTransition(async () => {
      const result = await increaseRoundOrRebid(bidding.id, userId, 'round_increase')

      if (result.success) {
        const successResult = result as { success: true; message: string; biddingId: number; biddingNumber: string }
        toast({
          title: "성공",
          description: successResult.message,
        })
        router.push(`/evcp/bid`)
        onSuccess()
      } else {
        const errorResult = result as { success: false; error: string }
        toast({
          title: "오류",
          description: errorResult.error || "차수증가 중 오류가 발생했습니다.",
          variant: 'destructive',
        })
      }
    })
  }

  return (
    <>
      <div className="flex items-center gap-2">
        {/* 상태별 액션 버튼 */}
        {/* 차수증가: 입찰공고 또는 입찰평가중 상태에서만 */}
        {(bidding.status === 'evaluation_of_bidding' || bidding.status === 'bidding_opened') && (
          <Button
            variant="outline"
            size="sm"
            onClick={() => setIsRoundIncreaseDialogOpen(true)}
            disabled={isPending}
          >
            <RotateCw className="mr-2 h-4 w-4" />
            차수증가
          </Button>
        )}

        {/* 유찰/낙찰: 입찰공고 또는 입찰평가중 상태에서만 */}
        {(bidding.status === 'bidding_opened' || bidding.status === 'evaluation_of_bidding') && (
          <>
            <Button
              variant="destructive"
              size="sm"
              onClick={handleMarkAsDisposal}
              disabled={isPending}
            >
              <XCircle className="mr-2 h-4 w-4" />
              유찰
            </Button>
            <Button
              variant="default"
              size="sm"
              onClick={onOpenAwardDialog}
              disabled={isPending}
            >
              <Trophy className="mr-2 h-4 w-4" />
              낙찰
            </Button>
          </>
        )}

        {/* 발주비율 취소: 100% 낙찰된 벤더가 있는 경우 */}
        {winnerVendor && (
          <Button
            variant="outline"
            size="sm"
            onClick={() => setIsCancelAwardDialogOpen(true)}
            disabled={isPending}
          >
            <RotateCcw className="mr-2 h-4 w-4" />
            발주비율 취소
          </Button>
        )}
        {/* 구분선 */}
        {(bidding.status === 'bidding_generated' || 
          bidding.status === 'bidding_disposal') && (
          <div className="h-4 w-px bg-border mx-1" />
        )}
            <Button
              variant="outline"
              size="sm"
              onClick={handleDocumentUpload}
            >
              <FileText className="mr-2 h-4 w-4" />
              입찰문서 등록
            </Button>
          
      </div>

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

      <BiddingDocumentUploadDialog
        open={isDocumentDialogOpen}
        onOpenChange={setIsDocumentDialogOpen}
        biddingId={biddingId}
        userId={userId}
        onSuccess={onSuccess}
      />

      {/* 차수증가 확인 다이얼로그 */}
      <Dialog open={isRoundIncreaseDialogOpen} onOpenChange={setIsRoundIncreaseDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>차수증가 확인</DialogTitle>
            <DialogDescription>
              입찰을 차수증가 처리하시겠습니까? 차수증가 후 입찰 관리 페이지로 이동합니다.
            </DialogDescription>
          </DialogHeader>
          <DialogFooter>
            <Button
              variant="outline"
              onClick={() => setIsRoundIncreaseDialogOpen(false)}
            >
              아니오
            </Button>
            <Button
              onClick={async () => {
                setIsRoundIncreaseDialogOpen(false)
                await handleRoundIncreaseWithNavigation()
              }}
              disabled={isPending}
            >
              예
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>

      {/* 발주비율 취소 확인 다이얼로그 */}
      <Dialog open={isCancelAwardDialogOpen} onOpenChange={setIsCancelAwardDialogOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>발주비율 취소 확인</DialogTitle>
            <DialogDescription>
              {winnerVendor && (
                <>
                  <strong>{winnerVendor.vendorName}</strong> 업체의 발주비율(100%)을 취소하시겠습니까?
                  <br />
                  취소 후 다른 업체의 발주비율을 설정할 수 있습니다.
                </>
              )}
            </DialogDescription>
          </DialogHeader>
          <DialogFooter>
            <Button
              variant="outline"
              onClick={() => setIsCancelAwardDialogOpen(false)}
            >
              아니오
            </Button>
            <Button
              variant="destructive"
              onClick={handleCancelAward}
              disabled={isPending}
            >
              취소하기
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>

    </>
  )
}
