"use client"

import React from "react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogFooter
} from "@/components/ui/dialog"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import { Button } from "@/components/ui/button"
import { Progress } from "@/components/ui/progress"
import {
  Upload,
  FileText,
  X,
  Loader2,
  CheckCircle
} from "lucide-react"
import { toast } from "sonner"
import { useSession } from "next-auth/react"
import {
  createUploadRevisionSchema,
  getUsageOptions,
  getUsageTypeOptions,
  getRevisionGuide,
  B3RevisionInput
} from "./revision-validation"

// 기존 메인 컴포넌트에서 추가할 import
// import { NewRevisionDialog } from "./new-revision-dialog"

/* -------------------------------------------------------------------------------------------------
 * Schema & Types
 * -----------------------------------------------------------------------------------------------*/








interface NewRevisionDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  documentId: number
  documentTitle?: string
  drawingKind: string
  onSuccess?: (result?: unknown) => void
}

/* -------------------------------------------------------------------------------------------------
 * File Upload Component
 * -----------------------------------------------------------------------------------------------*/
function FileUploadArea({ 
  files, 
  onFilesChange 
}: { 
  files: File[]
  onFilesChange: (files: File[]) => void 
}) {
  const fileInputRef = React.useRef<HTMLInputElement>(null)

  // 파일 검증 함수
  const validateFiles = (filesToValidate: File[]): { valid: File[], invalid: string[] } => {
    const MAX_FILE_SIZE = 1024 * 1024 * 1024 // 1GB
    const FORBIDDEN_EXTENSIONS = ['exe', 'com', 'dll', 'vbs', 'js', 'asp', 'aspx', 'bat', 'cmd']
    
    const valid: File[] = []
    const invalid: string[] = []
    
    filesToValidate.forEach(file => {
      // 파일 크기 검증
      if (file.size > MAX_FILE_SIZE) {
        invalid.push(`${file.name}: 파일 크기가 1GB를 초과합니다 (${formatFileSize(file.size)})`)
        return
      }
      
      // 파일 확장자 검증
      const extension = file.name.split('.').pop()?.toLowerCase()
      if (extension && FORBIDDEN_EXTENSIONS.includes(extension)) {
        invalid.push(`${file.name}: 금지된 파일 형식입니다 (.${extension})`)
        return
      }
      
      valid.push(file)
    })
    
    return { valid, invalid }
  }

  const handleFileSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
    const selectedFiles = Array.from(event.target.files || [])
    if (selectedFiles.length > 0) {
      const { valid, invalid } = validateFiles(selectedFiles)
      
      if (invalid.length > 0) {
        invalid.forEach(msg => toast.error(msg))
      }
      
      if (valid.length > 0) {
        onFilesChange([...files, ...valid])
      }
    }
  }

  const handleDrop = (event: React.DragEvent<HTMLDivElement>) => {
    event.preventDefault()
    const droppedFiles = Array.from(event.dataTransfer.files)
    if (droppedFiles.length > 0) {
      const { valid, invalid } = validateFiles(droppedFiles)
      
      if (invalid.length > 0) {
        invalid.forEach(msg => toast.error(msg))
      }
      
      if (valid.length > 0) {
        onFilesChange([...files, ...valid])
      }
    }
  }

  const handleDragOver = (event: React.DragEvent<HTMLDivElement>) => {
    event.preventDefault()
  }

  const removeFile = (index: number) => {
    onFilesChange(files.filter((_, i) => i !== index))
  }

  const formatFileSize = (bytes: number) => {
    if (bytes === 0) return '0 Bytes'
    const k = 1024
    const sizes = ['Bytes', 'KB', 'MB', 'GB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  }

  return (
    <div className="space-y-4">
      <div
        className="border-2 border-dashed border-border rounded-lg p-6 text-center cursor-pointer hover:border-border transition-colors"
        onDrop={handleDrop}
        onDragOver={handleDragOver}
        onClick={() => fileInputRef.current?.click()}
      >
        <Upload className="mx-auto h-12 w-12 text-muted-foreground mb-4" />
        <p className="text-sm text-muted-foreground mb-2">
          Drag files here or click to select
        </p>
        <p className="text-xs text-muted-foreground">
          Supports PDF, Word, Excel, Image, Text, ZIP files (max 1GB)
        </p>
        <p className="text-xs text-orange-600 mt-1">
          Note: File names cannot contain these characters: &lt; &gt; : &quot; &apos; | ? *
        </p>
        <p className="text-xs text-red-600 mt-1 font-medium">
          Forbidden file types: .exe, .com, .dll, .vbs, .js, .asp, .aspx, .bat, .cmd
        </p>
        <input
          ref={fileInputRef}
          type="file"
          multiple
          accept=".pdf,.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png,.gif,.txt,.zip"
          onChange={handleFileSelect}
          className="hidden"
        />
      </div>

      {files.length > 0 && (
        <div className="space-y-2 max-h-40 overflow-y-auto overscroll-contain pr-2">
          <p className="text-sm font-medium">Selected Files ({files.length})</p>
          <div className="max-h-40 overflow-y-auto space-y-2">
            {files.map((file, index) => (
              <div
                key={index}
                className="flex items-center justify-between p-2 bg-muted/50 rounded border"
              >
                <div className="flex items-center space-x-2 flex-1">
                  <FileText className="h-4 w-4 text-muted-foreground" />
                  <div className="flex-1 min-w-0">
                    <p className="text-sm font-medium truncate" title={file.name}>
                      {file.name}
                    </p>
                    <p className="text-xs text-muted-foreground">
                      {formatFileSize(file.size)}
                    </p>
                  </div>
                </div>
                <Button
                  type="button"
                  variant="ghost"
                  size="sm"
                  onClick={() => removeFile(index)}
                  className="h-8 w-8 p-0"
                >
                  <X className="h-4 w-4" />
                </Button>
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  )
}


/* -------------------------------------------------------------------------------------------------
 * Main Dialog Component
 * -----------------------------------------------------------------------------------------------*/
export function NewRevisionDialog({
  open,
  onOpenChange,
  documentId,
  documentTitle,
  drawingKind,
  onSuccess
}: NewRevisionDialogProps) {
  const [isUploading, setIsUploading] = React.useState(false)
  const [uploadProgress, setUploadProgress] = React.useState(0)
  const { data: session } = useSession()
  const [nextSerialNo, setNextSerialNo] = React.useState<string>("1")
  const [isLoadingSerialNo, setIsLoadingSerialNo] = React.useState(false)

    // Serial No 조회
    const fetchNextSerialNo = React.useCallback(async () => {
      console.log('🔍 fetchNextSerialNo called with documentId:', documentId)
      setIsLoadingSerialNo(true)
      try {
        const apiUrl = `/api/revisions/max-serial-no?documentId=${documentId}`
        console.log('🔍 Calling API:', apiUrl)

        const response = await fetch(apiUrl)
        console.log('🔍 API Response status:', response.status)

        if (response.ok) {
          const data = await response.json()
          console.log('🔍 API Response data:', data)
          console.log('🔍 data.nextSerialNo:', data.nextSerialNo)

          const serialNoString = String(data.nextSerialNo)
          console.log('🔍 Setting nextSerialNo to:', serialNoString)

          setNextSerialNo(serialNoString)
          console.log('🔍 nextSerialNo state updated')
        } else {
          console.error('🔍 API call failed with status:', response.status)
        }
      } catch (error) {
        console.error('❌ Failed to fetch serial no:', error)
        // 에러 시 기본값 1 사용
        setNextSerialNo("1")
      } finally {
        setIsLoadingSerialNo(false)
      }
    }, [documentId])

      // Dialog 열릴 때 Serial No 조회
  React.useEffect(() => {
    console.log('🎯 useEffect triggered - open:', open, 'documentId:', documentId)
    if (open && documentId) {
      console.log('🎯 Calling fetchNextSerialNo')
      fetchNextSerialNo()
    } else {
      console.log('🎯 Conditions not met for fetchNextSerialNo')
    }
  }, [open, documentId, fetchNextSerialNo])


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

  // drawingKind에 따른 동적 스키마 및 옵션 생성
  const revisionUploadSchema = React.useMemo(() => createUploadRevisionSchema(drawingKind), [drawingKind])
  const usageOptions = React.useMemo(() => getUsageOptions(drawingKind), [drawingKind])
  const showUsageType = drawingKind === 'B3'

  type RevisionUploadSchema = z.infer<typeof revisionUploadSchema>

  const form = useForm<RevisionUploadSchema>({
    resolver: zodResolver(revisionUploadSchema),
    defaultValues: {
      usage: "",
      revision: "",
      comment: "",
      usageType: showUsageType ? "" : undefined,
      attachments: [],
    },
  })

  const watchedFiles = form.watch("attachments")
  const watchedUsage = form.watch("usage")

  // 용도 선택에 따른 용도 타입 옵션 업데이트
  const usageTypeOptions = React.useMemo(() => {
    if (drawingKind === 'B3' && watchedUsage) {
      return getUsageTypeOptions(watchedUsage)
    }
    return []
  }, [drawingKind, watchedUsage])

  // 용도 변경 시 용도 타입 초기화 또는 자동 설정
  React.useEffect(() => {
    if (showUsageType && watchedUsage) {
      if (watchedUsage === "Comments") {
        form.setValue("usageType", "Comments")
      } else {
        form.setValue("usageType", "")
      }
    }
  }, [watchedUsage, showUsageType, form])

  // 리비전 가이드 텍스트
  const revisionGuide = React.useMemo(() => {
    return getRevisionGuide(drawingKind)
  }, [drawingKind])

  const handleDialogClose = () => {
    if (!isUploading) {
      form.reset()
      setUploadProgress(0)
      onOpenChange(false)
    }
  }

  const onSubmit = async (data: RevisionUploadSchema) => {
    console.log('🚀 onSubmit called with data:', data)
    console.log('🚀 Current nextSerialNo state:', nextSerialNo)
    console.log('🚀 documentId:', documentId)

    setIsUploading(true)
    setUploadProgress(0)

    try {
      const formData = new FormData()
      formData.append("documentId", String(documentId))
      formData.append("serialNo", nextSerialNo) // 추가
      console.log('🚀 Appending serialNo to formData:', nextSerialNo)

      formData.append("usage", data.usage)
      formData.append("revision", data.revision)
      formData.append("uploaderName", userName || "evcp")
      
      if (data.comment) {
        formData.append("comment", data.comment)
      }

      // B3인 경우에만 usageType 추가
      if (showUsageType && 'usageType' in data && data.usageType) {
        formData.append("usageType", data.usageType)
      }

      // 파일들 추가
      data.attachments.forEach((file) => {
        formData.append("attachments", file)
      })

      // 진행률 업데이트 시뮬레이션
      const totalSize = data.attachments.reduce((sum, file) => sum + file.size, 0)
      let uploadedSize = 0
      
      const progressInterval = setInterval(() => {
        uploadedSize += totalSize * 0.1
        const progress = Math.min((uploadedSize / totalSize) * 100, 90)
        setUploadProgress(progress)
      }, 300)

      const response = await fetch('/api/revision-upload-ship', {
        method: 'POST',
        body: formData,
      })

      clearInterval(progressInterval)

      if (!response.ok) {
        const errorData = await response.json()
        throw new Error(errorData.error || errorData.details || 'Upload failed.')
      }

      const result = await response.json()
      setUploadProgress(100)

      toast.success(
        result.message || 
        `Revision ${data.revision} uploaded successfully. (${result.data?.uploadedFiles?.length || 0} files)`
      )

      console.log('✅ Upload successful:', result)
      
      setTimeout(() => {
        handleDialogClose()
        onSuccess?.(result)
      }, 1000)

    } catch (error) {
      console.error('❌ Upload error:', error)
      
      let userMessage = "An error occurred during upload"
      
      if (error instanceof Error) {
        const message = error.message.toLowerCase()
        
        // 파일명 관련 에러
        if (message.includes("안전하지 않은 파일명") || message.includes("unsafe filename") || 
            message.includes("filename") && message.includes("invalid")) {
          userMessage = "File name contains invalid characters. Please avoid using < > : \" ' | ? * in file names. filename can't start with '..'."
        }
        // 파일명 길이 에러
        else if (message.includes("파일명이 너무 깁니다") || message.includes("filename too long") ||
                 message.includes("파일명") && message.includes("길이")) {
          userMessage = "File name is too long. Please use a shorter name (max 255 characters)."
        }
        // 파일 크기 에러
        else if (message.includes("파일 크기가 너무 큽니다") || message.includes("file size") || 
                 message.includes("1gb limit") || message.includes("exceeds") && message.includes("limit")) {
          userMessage = "File size is too large. Please use files smaller than 1GB."
        }
        // 클라이언트측 네트워크 에러
        else if (message.includes("network") || message.includes("fetch") || 
                 message.includes("connection") || message.includes("timeout")) {
          userMessage = "Network error occurred. Please check your connection and try again."
        }
        // 서버측 오류는 보안상 일반적인 메시지로 처리
        else if (message.includes("500") || message.includes("server") || 
                 message.includes("database") || message.includes("internal") ||
                 message.includes("security") || message.includes("validation")) {
          userMessage = "Please try again later. If the problem persists, please contact the administrator."
        }
        else {
          userMessage = "Please try again later. If the problem persists, please contact the administrator."
        }
      }
      
      toast.error(userMessage)
    } finally {
      setIsUploading(false)
      setTimeout(() => setUploadProgress(0), 2000)
    }
  }

  return (
    <Dialog open={open} onOpenChange={handleDialogClose}>
      <DialogContent className="max-w-2xl h-[90vh] flex flex-col overflow-hidden" style={{maxHeight:'90vh'}}>
        {/* 고정 헤더 */}
        <DialogHeader className="flex-shrink-0 pb-4 border-b">
          <DialogTitle className="flex items-center gap-2">
            <Upload className="h-5 w-5" />
            Upload New Revision
          </DialogTitle>
          {documentTitle && (
            <DialogDescription className="text-sm space-y-1">
              <div>Document: {documentTitle}</div>
              <div className="text-xs text-muted-foreground">
                Drawing Type: {drawingKind} | Serial No: {nextSerialNo}
                {isLoadingSerialNo && (
                  <>
                    <Loader2 className="inline-block ml-2 h-3 w-3 animate-spin" />
                    <span className="ml-1">Loading...</span>
                  </>
                )}
                {/* 디버그용 임시 표시 */}
                <div className="mt-1 text-xs text-orange-600">
                  Debug: nextSerialNo={nextSerialNo}, isLoading={isLoadingSerialNo}
                </div>
              </div>
            </DialogDescription>
          )}
        </DialogHeader>

        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col flex-1 overflow-hidden">
            {/* 스크롤 가능한 중간 영역 */}
            <div className="flex-1 overflow-y-auto px-1 py-4 space-y-6">
              {/* 용도 선택 */}
              <FormField
                control={form.control}
                name="usage"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="required">Usage</FormLabel>
                    <Select onValueChange={field.onChange} value={field.value}>
                      <FormControl>
                        <SelectTrigger>
                          <SelectValue placeholder="Select usage" />
                        </SelectTrigger>
                      </FormControl>
                      <SelectContent>
                        {usageOptions.map((option) => (
                          <SelectItem key={option.value} value={option.value}>
                            {option.label}
                          </SelectItem>
                        ))}
                      </SelectContent>
                    </Select>
                    <FormMessage />
                  </FormItem>
                )}
              />

              {/* 용도 타입 선택 (B3만, Comments가 아닐 때만) */}
              {showUsageType && watchedUsage && watchedUsage !== "Comments" && (
                <FormField
                  control={form.control}
                  name="usageType"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel className="required">Usage Type</FormLabel>
                      <Select onValueChange={field.onChange} value={field.value}>
                        <FormControl>
                          <SelectTrigger>
                            <SelectValue placeholder="Select usage type" />
                          </SelectTrigger>
                        </FormControl>
                        <SelectContent>
                          {usageTypeOptions.map((option) => (
                            <SelectItem key={option.value} value={option.value}>
                              {option.label}
                            </SelectItem>
                          ))}
                        </SelectContent>
                      </Select>
                      <FormMessage />
                    </FormItem>
                  )}
                />
              )}

              {/* 리비전 입력 */}
              <FormField
                control={form.control}
                name="revision"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="required">Revision</FormLabel>
                    <FormControl>
                      {drawingKind === 'B3' ? (
                        <B3RevisionInput
                          value={field.value}
                          onChange={field.onChange}
                          error={form.formState.errors.revision?.message}
                        />
                      ) : (
                        <>
                          <Input
                            placeholder={revisionGuide.placeholder}
                            {...field}
                            onChange={(e) => {
                              const upperValue = e.target.value.toUpperCase()
                              if (upperValue.length <= 3) {
                                field.onChange(upperValue)
                              }
                            }}
                          />
                          <div className="text-xs text-muted-foreground mt-1">
                            {revisionGuide.helpText}
                          </div>
                        </>
                      )}
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              {/* 코멘트 */}
              <FormField
                control={form.control}
                name="comment"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>Comment</FormLabel>
                    <FormControl>
                      <Textarea
                        placeholder="Enter description or changes for this revision (optional)"
                        className="resize-none"
                        rows={3}
                        {...field}
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              {/* 파일 업로드 */}
              <FormField
                control={form.control}
                name="attachments"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="required">Attachments</FormLabel>
                    <FormControl>
                      <FileUploadArea
                        files={watchedFiles || []}
                        onFilesChange={field.onChange}
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              {/* 업로드 진행률 */}
              {isUploading && (
                <div className="space-y-2">
                  <div className="flex items-center justify-between text-sm">
                    <span>Upload Progress</span>
                    <span>{uploadProgress.toFixed(0)}%</span>
                  </div>
                  <Progress value={uploadProgress} className="w-full" />
                  {uploadProgress === 100 && (
                    <div className="flex items-center gap-2 text-sm text-green-600">
                      <CheckCircle className="h-4 w-4" />
                      <span>Upload Complete</span>
                    </div>
                  )}
                </div>
              )}
            </div>

            {/* 고정 버튼 영역 */}
            <DialogFooter>
              <Button
                type="button"
                variant="outline"
                onClick={handleDialogClose}
                disabled={isUploading}
              >
                Cancel
              </Button>
              <Button
                type="submit"
                disabled={isUploading || !form.formState.isValid}
                className="min-w-[120px]"
              >
                {isUploading ? (
                  <>
                    <Loader2 className="mr-2 h-4 w-4 animate-spin" />
                    Uploading...
                  </>
                ) : (
                  <>
                    <Upload className="mr-2 h-4 w-4" />
                    Upload
                  </>
                )}
              </Button>
            </DialogFooter>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  )
}