"use client" import { useState } from 'react' import { useRouter } from 'next/navigation' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Label } from '@/components/ui/label' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Switch } from '@/components/ui/switch' import { X, Plus, Upload } from 'lucide-react' import { useToast } from '@/hooks/use-toast' import { useFileUpload } from '@/hooks/use-file-upload' import type { Artist } from '@/types/database' const artistFormSchema = z.object({ name: z.string().min(1, 'Name is required'), bio: z.string().min(10, 'Bio must be at least 10 characters'), specialties: z.array(z.string()).min(1, 'At least one specialty is required'), instagramHandle: z.string().optional(), hourlyRate: z.number().min(0).optional(), isActive: z.boolean().default(true), email: z.string().email().optional(), }) type ArtistFormData = z.infer interface ArtistFormProps { artist?: Artist onSuccess?: () => void } export function ArtistForm({ artist, onSuccess }: ArtistFormProps) { const router = useRouter() const { toast } = useToast() const [isSubmitting, setIsSubmitting] = useState(false) const [newSpecialty, setNewSpecialty] = useState('') const { uploadFiles, progress, isUploading, error: uploadError, clearProgress } = useFileUpload({ maxFiles: 10, maxSize: 5 * 1024 * 1024, // 5MB allowedTypes: ['image/jpeg', 'image/png', 'image/webp'], }) const { register, handleSubmit, watch, setValue, formState: { errors } } = useForm({ resolver: zodResolver(artistFormSchema), defaultValues: { name: artist?.name || '', bio: artist?.bio || '', specialties: artist?.specialties ? (typeof artist.specialties === 'string' ? JSON.parse(artist.specialties) : artist.specialties) : [], instagramHandle: artist?.instagramHandle || '', hourlyRate: artist?.hourlyRate || undefined, isActive: artist?.isActive !== false, email: '', } }) const specialties = watch('specialties') const addSpecialty = () => { if (newSpecialty.trim() && !specialties.includes(newSpecialty.trim())) { setValue('specialties', [...specialties, newSpecialty.trim()]) setNewSpecialty('') } } const removeSpecialty = (specialty: string) => { setValue('specialties', specialties.filter(s => s !== specialty)) } const handleFileUpload = async (files: FileList | null) => { if (!files || files.length === 0) return const fileArray = Array.from(files) await uploadFiles(fileArray, { keyPrefix: artist ? `portfolio/${artist.id}` : 'temp-portfolio' }) } const onSubmit = async (data: ArtistFormData) => { setIsSubmitting(true) try { const url = artist ? `/api/artists/${artist.id}` : '/api/artists' const method = artist ? 'PUT' : 'POST' const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }) if (!response.ok) { const error = await response.json() throw new Error(error.error || 'Failed to save artist') } const result = await response.json() toast({ title: 'Success', description: artist ? 'Artist updated successfully' : 'Artist created successfully', }) onSuccess?.() if (!artist) { router.push(`/admin/artists/${result.artist.id}`) } } catch (error) { console.error('Form submission error:', error) toast({ title: 'Error', description: error instanceof Error ? error.message : 'Failed to save artist', variant: 'destructive', }) } finally { setIsSubmitting(false) } } return (
{artist ? 'Edit Artist' : 'Create New Artist'}
{/* Basic Information */}
{errors.name && (

{errors.name.message}

)}
{errors.email && (

{errors.email.message}

)}
{/* Bio */}