"use client"

import * as React from "react"
import { useForm, useFieldArray } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { format } from "date-fns"
import { CalendarIcon, Loader2, Trash2, PlusCircle } from "lucide-react"
import { useSession } from "next-auth/react"

import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
  FormDescription,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"
import { Calendar } from "@/components/ui/calendar"
import { cn } from "@/lib/utils"
import { toast } from "sonner"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Separator } from "@/components/ui/separator"
import { createPreQuoteRfqAction } from "@/lib/bidding/pre-quote/service"
import { PurchaseGroupCodeSelector } from "@/components/common/selectors/purchase-group-code/purchase-group-code-selector"
import type { PurchaseGroupCodeWithUser } from "@/components/common/selectors/purchase-group-code"
import { getBiddingById } from "@/lib/bidding/service"
import { getProjectIdByCodeAndName } from "@/lib/bidding/manage/project-utils"

// 아이템 스키마
const itemSchema = z.object({
  itemCode: z.string().optional(),
  itemName: z.string().optional(),
  materialCode: z.string().optional(),
  materialName: z.string().optional(),
  quantity: z.number().min(1, "수량은 1 이상이어야 합니다"),
  uom: z.string().min(1, "단위를 입력해주세요"),
  totalWeight: z.union([z.number(), z.string(), z.null()]).optional(), // 중량 추가
  weightUnit: z.string().optional().nullable(), // 중량단위 추가
  remark: z.string().optional(),
})

// 사전견적용 일반견적 생성 폼 스키마
const createPreQuoteRfqSchema = z.object({
  rfqType: z.string().optional(),
  rfqTitle: z.string().min(1, "견적명을 입력해주세요"),
  dueDate: z.date({
    required_error: "제출마감일을 선택해주세요",
  }).optional(), // 필수값 해제
  picUserId: z.number().optional(),
  projectId: z.number().optional(),
  remark: z.string().optional(),
  biddingNumber: z.string().optional(), // 입찰 No. 추가
  contractStartDate: z.date().optional(), // 계약기간 시작
  contractEndDate: z.date().optional(), // 계약기간 종료
  items: z.array(itemSchema).min(1, "최소 하나의 자재를 추가해주세요"),
})

type CreatePreQuoteRfqFormValues = z.infer<typeof createPreQuoteRfqSchema>

interface CreatePreQuoteRfqDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  biddingId: number
  biddingItems: Array<{
    id: number
    materialGroupNumber?: string | null
    materialGroupInfo?: string | null
    materialNumber?: string | null
    materialInfo?: string | null
    quantity?: string | null
    quantityUnit?: string | null
    totalWeight?: string | null
    weightUnit?: string | null
  }>
  picUserId?: number | null
  biddingConditions?: {
    paymentTerms?: string | null
    taxConditions?: string | null
    incoterms?: string | null
    incotermsOption?: string | null
    contractDeliveryDate?: string | null
    shippingPort?: string | null
    destinationPort?: string | null
    isPriceAdjustmentApplicable?: boolean | null
    sparePartOptions?: string | null
  } | null
  onSuccess?: () => void
}

export function CreatePreQuoteRfqDialog({
  open,
  onOpenChange,
  biddingId,
  biddingItems,
  picUserId,
  biddingConditions,
  onSuccess
}: CreatePreQuoteRfqDialogProps) {
  const [isLoading, setIsLoading] = React.useState(false)
  const [selectedBidPic, setSelectedBidPic] = React.useState<PurchaseGroupCodeWithUser | undefined>(undefined)
  const { data: session } = useSession()

  const userId = React.useMemo(() => {
    return session?.user?.id ? Number(session.user.id) : null;
  }, [session]);

  // 입찰품목을 일반견적 아이템으로 매핑
  const initialItems = React.useMemo(() => {
    return biddingItems.map((item) => ({
      itemCode: item.materialGroupNumber || "",
      itemName: item.materialGroupInfo || "",
      materialCode: item.materialNumber || "",
      materialName: item.materialInfo || "",
      quantity: item.quantity ? parseFloat(item.quantity) : 1,
      uom: item.quantityUnit || item.weightUnit || "EA",
      totalWeight: item.totalWeight ? parseFloat(item.totalWeight) : null,
      weightUnit: item.weightUnit || null,
      remark: "",
    }))
  }, [biddingItems])

  const form = useForm<CreatePreQuoteRfqFormValues>({
    resolver: zodResolver(createPreQuoteRfqSchema),
    defaultValues: {
      rfqType: "",
      rfqTitle: "",
      dueDate: undefined,
      picUserId: undefined,
      projectId: undefined,
      remark: "",
      items: initialItems.length > 0 ? initialItems : [
        {
          itemCode: "",
          itemName: "",
          materialCode: "",
          materialName: "",
          quantity: 1,
          uom: "",
          totalWeight: null,
          weightUnit: null,
          remark: "",
        },
      ],
    },
  })

  /*
  const { fields, append, remove } = useFieldArray({
    control: form.control,
    name: "items",
  })
  */

  // 견적담당자 정보 로드
  React.useEffect(() => {
    const loadBiddingInfo = async () => {
      if (!biddingId || !open) return

      try {
        const bidding = await getBiddingById(biddingId)
        if (bidding) {
          setSelectedBidPic({
            DISPLAY_NAME: bidding.bidPicName || '',
            PURCHASE_GROUP_CODE: bidding.bidPicCode || '',
            EMPLOYEE_NUMBER: '',
            user: bidding.bidPicId ? {
              id: bidding.bidPicId,
              name: bidding.bidPicName || '',
              email: '',
              employeeNumber: null
            } : undefined
          })
        }
      } catch (error) {
        console.error('Failed to load bidding info:', error)
      }
    }

    loadBiddingInfo()
  }, [biddingId, open])
  
  // 프로젝트 정보 상태 추가
  const [projectInfo, setProjectInfo] = React.useState<string>("")

  // 다이얼로그가 열릴 때 폼 초기화
  React.useEffect(() => {
    if (open) {
      // 입찰 정보를 기반으로 기본값 설정
      let rfqTitle = "";
      let projectId: number | undefined = undefined;
      let contractStartDate: Date | undefined = undefined;
      let contractEndDate: Date | undefined = undefined;
      let biddingNumber = "";

      const loadDetailedBiddingInfo = async () => {
        if (biddingId) {
          try {
            const bidding = await getBiddingById(biddingId)
            if (bidding) {
              rfqTitle = bidding.title;
              biddingNumber = bidding.biddingNumber;
              
              // 프로젝트 정보 설정
              const pCode = bidding.projectCode || "";
              const pName = bidding.projectName || "";
              setProjectInfo(pCode && pName ? `${pCode} - ${pName}` : pCode || pName || "");

              // 프로젝트 ID 조회
              if (pCode && pName) {
                const fetchedProjectId = await getProjectIdByCodeAndName(pCode, pName)
                if (fetchedProjectId) {
                  form.setValue("projectId", fetchedProjectId)
                }
              }

              // 폼 값 설정
              form.setValue("rfqTitle", rfqTitle);
              form.setValue("rfqType", "pre_bidding"); // 기본값 설정
              if (biddingNumber) form.setValue("biddingNumber", biddingNumber);
              
              if (bidding.contractStartDate) form.setValue("contractStartDate", new Date(bidding.contractStartDate));
              if (bidding.contractEndDate) form.setValue("contractEndDate", new Date(bidding.contractEndDate));
            }
          } catch (e) {
            console.error(e);
          }
        }
      };
      loadDetailedBiddingInfo();

      form.reset({
        rfqType: "pre_bidding", // 기본값
        rfqTitle: "",
        dueDate: undefined, // 필수값 해제되었으므로 undefined 가능
        picUserId: selectedBidPic?.user?.id,
        projectId: undefined,
        remark: "",
        biddingNumber: "",
        contractStartDate: undefined,
        contractEndDate: undefined,
        items: initialItems.length > 0 ? initialItems : [
          {
            itemCode: "",
            itemName: "",
            materialCode: "",
            materialName: "",
            quantity: 1,
            uom: "",
            totalWeight: null,
            weightUnit: null,
            remark: "",
          },
        ],
      })
    }
  }, [open, initialItems, form, selectedBidPic, biddingId])

  // 견적 종류 변경
  const handleRfqTypeChange = (value: string) => {
    form.setValue("rfqType", value)
  }

  const handleCancel = () => {
    form.reset({
      rfqType: "",
      rfqTitle: "",
      dueDate: undefined,
      picUserId: undefined,
      projectId: undefined,
      remark: "",
      items: initialItems.length > 0 ? initialItems : [
        {
          itemCode: "",
          itemName: "",
          materialCode: "",
          materialName: "",
          quantity: 1,
          uom: "",
          totalWeight: null,
          weightUnit: null,
          remark: "",
        },
      ],
    })
      setSelectedBidPic(undefined)
    onOpenChange(false)
  }

  const onSubmit = async (data: CreatePreQuoteRfqFormValues) => {
    if (!userId) {
      toast.error("로그인이 필요합니다")
      return
    }



    const picUserId = selectedBidPic?.user?.id || session?.user?.id

    setIsLoading(true)
    
    try {
      // 서버 액션 호출 (입찰 조건 포함)
      const result = await createPreQuoteRfqAction({
        // biddingId, // createPreQuoteRfqAction 인터페이스 변경됨
        biddingId,
        rfqType: data.rfqType || "pre_bidding",
        rfqTitle: data.rfqTitle,
        dueDate: data.dueDate ? new Date(data.dueDate) : undefined, // optional이지만 submit시에는 값이 있을 수 있음 (없으면 서비스에서 처리)
        picUserId,
        projectId: data.projectId,
        remark: data.remark || "",
        biddingNumber: data.biddingNumber, // 추가
        contractStartDate: data.contractStartDate, // 추가
        contractEndDate: data.contractEndDate, // 추가
        items: data.items.map(item => ({
          itemCode: item.itemCode || "",
          itemName: item.itemName || "",
          materialCode: item.materialCode,
          materialName: item.materialName,
          quantity: item.quantity,
          uom: item.uom,
          totalWeight: item.totalWeight,
          weightUnit: item.weightUnit,
          remark: item.remark,
        })),
        biddingConditions: biddingConditions || undefined,
        createdBy: userId,
        updatedBy: userId,
      })

      if (result.success) {
        toast.success(result.message, {
          description: result.data?.rfqCode ? `RFQ 코드: ${result.data.rfqCode}` : undefined,
        })

        // 다이얼로그 닫기
        onOpenChange(false)
        
        // 성공 콜백 실행
        if (onSuccess) {
          onSuccess()
        }
      } else {
        toast.error(result.error || "사전견적용 일반견적 생성에 실패했습니다")
      }
      
    } catch (error) {
      console.error('사전견적용 일반견적 생성 오류:', error)
      toast.error("사전견적용 일반견적 생성에 실패했습니다", {
        description: "알 수 없는 오류가 발생했습니다",
      })
    } finally {
      setIsLoading(false)
    }
  }

  // 아이템 추가 (사용안함)
  /*
  const handleAddItem = () => {
    append({
      itemCode: "",
      itemName: "",
      materialCode: "",
      materialName: "",
      quantity: 1,
      uom: "",
      remark: "",
    })
  }
  */

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-4xl h-[90vh] flex flex-col">
        {/* 고정된 헤더 */}
        <DialogHeader className="flex-shrink-0">
          <DialogTitle>사전견적용 일반견적 생성</DialogTitle>
          <DialogDescription>
            입찰의 사전견적을 위한 일반견적을 생성합니다. 입찰품목이 자재정보로 매핑되어 있습니다.
          </DialogDescription>
        </DialogHeader>

        {/* 스크롤 가능한 컨텐츠 영역 */}
        <ScrollArea className="flex-1 px-1">
          <Form {...form}>
            <form id="createPreQuoteRfqForm" onSubmit={form.handleSubmit(onSubmit)} className="space-y-6 py-2">
              
              {/* 기본 정보 섹션 */}
              <div className="space-y-4">
                <h3 className="text-lg font-semibold">기본 정보</h3>
                
                <div className="grid grid-cols-2 gap-4">
                  {/* 견적 종류 */}
                  {/* <div className="space-y-2">
                    <FormField
                      control={form.control}
                      name="rfqType"
                      render={({ field }) => (
                        <FormItem className="flex flex-col">
                          <FormLabel>
                            견적 종류 <span className="text-red-500">*</span>
                          </FormLabel>
                          <FormControl>
                            <Input {...field} value="사전견적(입찰)" readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div> */}

                  {/* 제출마감일 */}
                  <FormField
                    control={form.control}
                    name="dueDate"
                    render={({ field }) => (
                      <FormItem className="flex flex-col">
                        <FormLabel>
                          제출마감일
                        </FormLabel>
                        <Popover>
                          <PopoverTrigger asChild>
                            <FormControl>
                              <Button
                                variant="outline"
                                className={cn(
                                  "w-full pl-3 text-left font-normal",
                                  !field.value && "text-muted-foreground"
                                )}
                              >
                                {field.value ? (
                                  format(field.value, "yyyy-MM-dd HH:mm")
                                ) : (
                                  <span>제출마감일을 선택하세요 (선택)</span>
                                )}
                                <CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
                              </Button>
                            </FormControl>
                          </PopoverTrigger>
                          <PopoverContent className="w-auto p-0" align="start">
                            <Calendar
                              mode="single"
                              selected={field.value}
                              onSelect={(date) => {
                                if (!date) {
                                  field.onChange(undefined)
                                  return
                                }
                                const newDate = new Date(date)
                                if (field.value) {
                                  newDate.setHours(field.value.getHours(), field.value.getMinutes())
                                } else {
                                  newDate.setHours(0, 0, 0, 0)
                                }
                                field.onChange(newDate)
                              }}
                              disabled={(date) => {
                                const today = new Date()
                                today.setHours(0, 0, 0, 0)
                                return date < today || date < new Date("1900-01-01")
                              }}
                              initialFocus
                            />
                            <div className="p-3 border-t border-border">
                              <Input
                                type="time"
                                value={field.value ? format(field.value, "HH:mm") : ""}
                                onChange={(e) => {
                                  if (field.value) {
                                    const [hours, minutes] = e.target.value.split(':').map(Number)
                                    const newDate = new Date(field.value)
                                    newDate.setHours(hours, minutes)
                                    field.onChange(newDate)
                                  }
                                }}
                              />
                            </div>
                          </PopoverContent>
                        </Popover>
                        <FormMessage />
                      </FormItem>
                    )}
                  />
                </div>

                {/* 견적명 */}
                <FormField
                  control={form.control}
                  name="rfqTitle"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>
                        견적명 <span className="text-red-500">*</span>
                      </FormLabel>
                      <FormControl>
                        <Input
                          placeholder="예: 입찰 사전견적용 일반견적"
                          {...field}
                        />
                      </FormControl>
                      <FormDescription>
                        견적의 목적이나 내용을 간단명료하게 입력해주세요
                      </FormDescription>
                      <FormMessage />
                    </FormItem>
                  )}
                />

                {/* 프로젝트 선택 */}
                <div className="space-y-2">
                    <FormItem className="flex flex-col">
                      <FormLabel>프로젝트</FormLabel>
                      <FormControl>
                        <Input
                          value={projectInfo}
                          readOnly
                          className="bg-muted"
                          placeholder="프로젝트 정보 없음"
                        />
                      </FormControl>
                    </FormItem>
                </div>
                <FormField
                  control={form.control}
                  name="projectId"
                  render={({ field }) => (
                    <input type="hidden" {...field} value={field.value || ''} />
                  )}
                />

                {/* 담당자 정보 */}
                <FormField
                  control={form.control}
                  name="picUserId"
                  render={({ field }) => (
                    <FormItem className="flex flex-col">
                      <FormLabel>
                        견적담당자 <span className="text-red-500">*</span>
                      </FormLabel>
                      <FormControl>
                        <PurchaseGroupCodeSelector
                          selectedCode={selectedBidPic}
                          onCodeSelect={(code) => {
                            setSelectedBidPic(code)
                            field.onChange(code.user?.id)
                          }}
                          placeholder="입찰담당자 선택"
                        />
                      </FormControl>
                      <FormDescription>
                        사전견적용 일반견적의 담당자를 선택합니다
                      </FormDescription>
                      <FormMessage />
                    </FormItem>
                  )}
                />


                {/* 계약기간 */}
                <div className="grid grid-cols-2 gap-4">
                  <FormField
                    control={form.control}
                    name="contractStartDate"
                    render={({ field }) => (
                      <FormItem className="flex flex-col">
                        <FormLabel>계약기간 시작</FormLabel>
                        <Popover>
                          <PopoverTrigger asChild>
                            <FormControl>
                              <Button
                                variant="outline"
                                className={cn(
                                  "w-full pl-3 text-left font-normal",
                                  !field.value && "text-muted-foreground"
                                )}
                              >
                                {field.value ? (
                                  format(field.value, "yyyy-MM-dd")
                                ) : (
                                  <span>시작일 선택</span>
                                )}
                                <CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
                              </Button>
                            </FormControl>
                          </PopoverTrigger>
                          <PopoverContent className="w-auto p-0" align="start">
                            <Calendar
                              mode="single"
                              selected={field.value}
                              onSelect={field.onChange}
                              initialFocus
                            />
                          </PopoverContent>
                        </Popover>
                        <FormMessage />
                      </FormItem>
                    )}
                  />

                  <FormField
                    control={form.control}
                    name="contractEndDate"
                    render={({ field }) => (
                      <FormItem className="flex flex-col">
                        <FormLabel>계약기간 종료</FormLabel>
                        <Popover>
                          <PopoverTrigger asChild>
                            <FormControl>
                              <Button
                                variant="outline"
                                className={cn(
                                  "w-full pl-3 text-left font-normal",
                                  !field.value && "text-muted-foreground"
                                )}
                              >
                                {field.value ? (
                                  format(field.value, "yyyy-MM-dd")
                                ) : (
                                  <span>종료일 선택</span>
                                )}
                                <CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
                              </Button>
                            </FormControl>
                          </PopoverTrigger>
                          <PopoverContent className="w-auto p-0" align="start">
                            <Calendar
                              mode="single"
                              selected={field.value}
                              onSelect={field.onChange}
                              initialFocus
                            />
                          </PopoverContent>
                        </Popover>
                        <FormMessage />
                      </FormItem>
                    )}
                  />
                </div>

                {/* 비고 */}
                <FormField
                  control={form.control}
                  name="remark"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>비고</FormLabel>
                      <FormControl>
                        <Textarea
                          placeholder="추가 비고사항을 입력하세요"
                          className="resize-none"
                          rows={3}
                          {...field}
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />
              </div>

              <Separator />

              {/* 아이템 정보 섹션 (자동 매핑되므로 UI 제거) */}
              {/* <div className="space-y-4">
                ...
              </div> */}
            </form>
          </Form>
        </ScrollArea>

        {/* 고정된 푸터 */}
        <DialogFooter className="flex-shrink-0">
          <Button
            type="button"
            variant="outline"
            onClick={handleCancel}
            disabled={isLoading}
          >
            취소
          </Button>
          <Button
            type="submit"
            form="createPreQuoteRfqForm"
            onClick={form.handleSubmit(onSubmit)}
            disabled={isLoading}
          >
            {isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
            {isLoading ? "생성 중..." : "사전견적용 일반견적 생성"}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

