"use client" import type React from "react" import { useSearchParams } from "next/navigation" import { useEffect } from "react" import { fetchFlashItem } from "@/hooks/use-flash" import { useState, useMemo } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Checkbox } from "@/components/ui/checkbox" import { Calendar } from "@/components/ui/calendar" import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { useFeatureFlag } from "@/components/feature-flags-provider" import { useArtists } from "@/hooks/use-artist-data" import { useAvailability } from "@/hooks/use-availability" import { CalendarIcon, DollarSign, MessageSquare, User, Loader2, CheckCircle2, XCircle, AlertCircle } from "lucide-react" import { format } from "date-fns" import Link from "next/link" const timeSlots = ["10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM"] const tattooSizes = [ { size: "Small (2-4 inches)", duration: "1-2 hours", price: "150-300" }, { size: "Medium (4-6 inches)", duration: "2-4 hours", price: "300-600" }, { size: "Large (6+ inches)", duration: "4-6 hours", price: "600-1000" }, { size: "Full Session", duration: "6-8 hours", price: "1000-1500" }, ] interface BookingFormProps { artistId?: string } export function BookingForm({ artistId }: BookingFormProps) { const search = useSearchParams() const flashIdParam = search?.get('flashId') || undefined const [step, setStep] = useState(1) const [selectedDate, setSelectedDate] = useState() // Fetch artists from API const { data: artists, isLoading: artistsLoading } = useArtists({ limit: 50 }) const [formData, setFormData] = useState({ // Personal Info firstName: "", lastName: "", email: "", phone: "", age: "", // Appointment Details artistId: artistId || "", preferredDate: "", preferredTime: "", alternateDate: "", alternateTime: "", // Tattoo Details tattooDescription: "", tattooSize: "", placement: "", isFirstTattoo: false, hasAllergies: false, allergyDetails: "", referenceImages: "", // Additional Info specialRequests: "", depositAmount: 100, agreeToTerms: false, agreeToDeposit: false, flashId: flashIdParam || "", }) const selectedArtist = artists?.find((a) => a.slug === formData.artistId) const selectedSize = tattooSizes.find((size) => size.size === formData.tattooSize) const bookingEnabled = useFeatureFlag("BOOKING_ENABLED") // Prefill from flash piece if provided useEffect(() => { const load = async () => { if (!flashIdParam) return const item = await fetchFlashItem(flashIdParam) if (!item) return setFormData((prev) => ({ ...prev, tattooDescription: [item.title, item.description].filter(Boolean).join(' - '), })) } load() }, [flashIdParam]) // Calculate appointment start and end times for availability checking const { appointmentStart, appointmentEnd } = useMemo(() => { if (!selectedDate || !formData.preferredTime || !selectedSize) { return { appointmentStart: null, appointmentEnd: null } } // Parse time slot (e.g., "2:00 PM") const timeParts = formData.preferredTime.match(/(\d+):(\d+)\s*(AM|PM)/i) if (!timeParts) return { appointmentStart: null, appointmentEnd: null } let hours = parseInt(timeParts[1]) const minutes = parseInt(timeParts[2]) const meridiem = timeParts[3].toUpperCase() if (meridiem === 'PM' && hours !== 12) hours += 12 if (meridiem === 'AM' && hours === 12) hours = 0 const start = new Date(selectedDate) start.setHours(hours, minutes, 0, 0) // Estimate duration from tattoo size (use max hours) const durationHours = parseInt(selectedSize.duration.split('-')[1] || selectedSize.duration.split('-')[0]) const end = new Date(start.getTime() + durationHours * 60 * 60 * 1000) return { appointmentStart: start.toISOString(), appointmentEnd: end.toISOString(), } }, [selectedDate, formData.preferredTime, selectedSize]) // Check availability in real-time const availability = useAvailability({ artistId: selectedArtist?.id || null, startTime: appointmentStart, endTime: appointmentEnd, enabled: !!selectedArtist && !!appointmentStart && !!appointmentEnd && step === 2, }) const handleInputChange = (field: string, value: any) => { setFormData((prev) => ({ ...prev, [field]: value })) } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!bookingEnabled) { // Safety: no-op when disabled return } // Handle form submission console.log("Booking submitted:", formData) // In a real app, this would send data to your backend } const nextStep = () => setStep((prev) => Math.min(prev + 1, 4)) const prevStep = () => setStep((prev) => Math.max(prev - 1, 1)) return (
{/* Header */}

Book Your Appointment

Let's create something amazing together. Fill out the form below to schedule your tattoo session.

{/* Progress Indicator */}
{[1, 2, 3, 4].map((stepNumber) => (
= stepNumber ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground" }`} > {stepNumber}
{stepNumber < 4 && (
stepNumber ? "bg-primary" : "bg-muted"}`} /> )}
))}
{/* Booking disabled notice */} {!bookingEnabled && (
Online booking is temporarily unavailable. Please {" "} contact the studio .
)}
{/* Step 1: Personal Information */} {step === 1 && ( Personal Information
handleInputChange("firstName", e.target.value)} required />
handleInputChange("lastName", e.target.value)} required />
handleInputChange("email", e.target.value)} required />
handleInputChange("phone", e.target.value)} required />
handleInputChange("age", e.target.value)} required />

Must be 18 or older

handleInputChange("isFirstTattoo", checked)} />
handleInputChange("hasAllergies", checked)} />
{formData.hasAllergies && (