"use client" import * as React from "react" import { Upload, X, FileVideo, FileImage, AlertCircle, Check } from "lucide-react" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { uploadFile, UPLOAD_CONFIGS, formatFileSize, UploadError } from "@/lib/storage" interface MediaUploadProps { type: "video" | "thumbnail" | "avatar" workoutId?: string value?: string onChange: (url: string) => void disabled?: boolean className?: string } export function MediaUpload({ type, workoutId, value, onChange, disabled = false, className, }: MediaUploadProps) { const [isDragging, setIsDragging] = React.useState(false) const [isUploading, setIsUploading] = React.useState(false) const [uploadProgress, setUploadProgress] = React.useState(0) const [error, setError] = React.useState(null) const fileInputRef = React.useRef(null) const config = UPLOAD_CONFIGS[type] const isVideo = type === "video" const Icon = isVideo ? FileVideo : FileImage const maxSizeMB = config.maxSize / (1024 * 1024) const handleDragOver = (e: React.DragEvent) => { e.preventDefault() if (!disabled) setIsDragging(true) } const handleDragLeave = (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) } const handleDrop = (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) if (disabled) return const files = e.dataTransfer.files if (files.length > 0) { handleFile(files[0]) } } const handleFileInput = (e: React.ChangeEvent) => { const files = e.target.files if (files && files.length > 0) { handleFile(files[0]) } } const handleFile = async (file: File) => { setError(null) setUploadProgress(0) // Generate temporary workout ID if not provided const targetWorkoutId = workoutId || "temp" try { setIsUploading(true) // Simulate progress updates const progressInterval = setInterval(() => { setUploadProgress((prev) => { if (prev >= 90) { clearInterval(progressInterval) return prev } return prev + 10 }) }, 200) const result = await uploadFile(file, config, targetWorkoutId) clearInterval(progressInterval) setUploadProgress(100) onChange(result.url) // Reset progress after a delay setTimeout(() => { setUploadProgress(0) setIsUploading(false) }, 500) } catch (err) { setIsUploading(false) setUploadProgress(0) if (err && typeof err === "object" && "code" in err) { const uploadError = err as UploadError setError(uploadError.message) } else { setError("Upload failed. Please try again.") } } } const handleClear = () => { onChange("") setError(null) if (fileInputRef.current) { fileInputRef.current.value = "" } } const handleClick = () => { if (!disabled && !isUploading) { fileInputRef.current?.click() } } // Preview for existing value if (value && !isUploading) { return (
{isVideo ? (

{value}

) } return (
{isUploading ? (

Uploading... {uploadProgress}%

) : ( <>

{isDragging ? "Drop file here" : "Click or drag file to upload"}

{isVideo ? "Video" : "Image"} up to {maxSizeMB} MB
{config.allowedTypes.map(t => t.split("/").pop()?.toUpperCase()).join(", ")}

)}
{error && (
{error}
)}
) }