'use client'

import { useState, useRef, useCallback } from 'react'
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { ProjectLogo } from './project-logo'
import { Upload, Loader2, Trash2, ImagePlus } from 'lucide-react'
import { trpc } from '@/lib/trpc/client'
import { toast } from 'sonner'

type LogoUploadProps = {
  project: {
    id: string
    title: string
    logoKey?: string | null
  }
  currentLogoUrl?: string | null
  onUploadComplete?: () => void
  children?: React.ReactNode
}

const MAX_SIZE_MB = 5
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']

export function LogoUpload({
  project,
  currentLogoUrl,
  onUploadComplete,
  children,
}: LogoUploadProps) {
  const [open, setOpen] = useState(false)
  const [preview, setPreview] = useState<string | null>(null)
  const [selectedFile, setSelectedFile] = useState<File | null>(null)
  const [isUploading, setIsUploading] = useState(false)
  const [isDeleting, setIsDeleting] = useState(false)
  const fileInputRef = useRef<HTMLInputElement>(null)

  const utils = trpc.useUtils()
  const getUploadUrl = trpc.logo.getUploadUrl.useMutation()
  const confirmUpload = trpc.logo.confirmUpload.useMutation()
  const deleteLogo = trpc.logo.delete.useMutation()

  const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0]
    if (!file) return

    // Validate type
    if (!ALLOWED_TYPES.includes(file.type)) {
      toast.error('Invalid file type. Please upload a JPEG, PNG, GIF, or WebP image.')
      return
    }

    // Validate size
    if (file.size > MAX_SIZE_MB * 1024 * 1024) {
      toast.error(`File too large. Maximum size is ${MAX_SIZE_MB}MB.`)
      return
    }

    setSelectedFile(file)

    // Create preview
    const reader = new FileReader()
    reader.onload = (e) => {
      setPreview(e.target?.result as string)
    }
    reader.readAsDataURL(file)
  }, [])

  const handleUpload = async () => {
    if (!selectedFile) return

    setIsUploading(true)
    try {
      // Get pre-signed upload URL (includes provider type for tracking)
      const { uploadUrl, key, providerType } = await getUploadUrl.mutateAsync({
        projectId: project.id,
        fileName: selectedFile.name,
        contentType: selectedFile.type,
      })

      // Upload file directly to storage
      const uploadResponse = await fetch(uploadUrl, {
        method: 'PUT',
        body: selectedFile,
        headers: {
          'Content-Type': selectedFile.type,
        },
      })

      if (!uploadResponse.ok) {
        throw new Error('Failed to upload file')
      }

      // Confirm upload with the provider type that was used
      await confirmUpload.mutateAsync({ projectId: project.id, key, providerType })

      // Invalidate logo query
      utils.logo.getUrl.invalidate({ projectId: project.id })

      toast.success('Logo updated successfully')
      setOpen(false)
      setPreview(null)
      setSelectedFile(null)
      onUploadComplete?.()
    } catch (error) {
      console.error('Upload error:', error)
      toast.error('Failed to upload logo. Please try again.')
    } finally {
      setIsUploading(false)
    }
  }

  const handleDelete = async () => {
    setIsDeleting(true)
    try {
      await deleteLogo.mutateAsync({ projectId: project.id })
      utils.logo.getUrl.invalidate({ projectId: project.id })
      toast.success('Logo removed')
      setOpen(false)
      onUploadComplete?.()
    } catch (error) {
      console.error('Delete error:', error)
      toast.error('Failed to remove logo')
    } finally {
      setIsDeleting(false)
    }
  }

  const handleCancel = () => {
    setPreview(null)
    setSelectedFile(null)
    setOpen(false)
  }

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        {children || (
          <Button variant="outline" size="sm" className="gap-2">
            <ImagePlus className="h-4 w-4" />
            {currentLogoUrl ? 'Change Logo' : 'Add Logo'}
          </Button>
        )}
      </DialogTrigger>
      <DialogContent className="sm:max-w-md">
        <DialogHeader>
          <DialogTitle>Update Project Logo</DialogTitle>
          <DialogDescription>
            Upload a logo for &quot;{project.title}&quot;. Allowed formats: JPEG, PNG, GIF, WebP.
            Max size: {MAX_SIZE_MB}MB.
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-4 py-4">
          {/* Preview */}
          <div className="flex justify-center">
            <ProjectLogo
              project={project}
              logoUrl={preview || currentLogoUrl}
              size="lg"
            />
          </div>

          {/* File input */}
          <div className="space-y-2">
            <Label htmlFor="logo">Select image</Label>
            <Input
              ref={fileInputRef}
              id="logo"
              type="file"
              accept={ALLOWED_TYPES.join(',')}
              onChange={handleFileSelect}
              className="cursor-pointer"
            />
          </div>
        </div>

        <DialogFooter className="flex-col gap-2 sm:flex-row">
          {currentLogoUrl && !preview && (
            <Button
              variant="destructive"
              onClick={handleDelete}
              disabled={isDeleting}
              className="w-full sm:w-auto"
            >
              {isDeleting ? (
                <Loader2 className="mr-2 h-4 w-4 animate-spin" />
              ) : (
                <Trash2 className="mr-2 h-4 w-4" />
              )}
              Remove
            </Button>
          )}

          <div className="flex gap-2 w-full sm:w-auto">
            <Button variant="outline" onClick={handleCancel} className="flex-1">
              Cancel
            </Button>
            <Button
              onClick={handleUpload}
              disabled={!selectedFile || isUploading}
              className="flex-1"
            >
              {isUploading ? (
                <Loader2 className="mr-2 h-4 w-4 animate-spin" />
              ) : (
                <Upload className="mr-2 h-4 w-4" />
              )}
              Upload
            </Button>
          </div>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}
