import { useState } from 'react'; import { ConsumerPreference, ProduceCategory, ProducePreference } from '../../lib/demand/types'; interface PreferencesFormProps { initialPreferences?: Partial; onSubmit: (preferences: Partial) => void; loading?: boolean; } const DIETARY_TYPES = [ { value: 'omnivore', label: 'Omnivore' }, { value: 'vegetarian', label: 'Vegetarian' }, { value: 'vegan', label: 'Vegan' }, { value: 'pescatarian', label: 'Pescatarian' }, { value: 'flexitarian', label: 'Flexitarian' }, ] as const; const PRODUCE_CATEGORIES: { value: ProduceCategory; label: string; icon: string }[] = [ { value: 'leafy_greens', label: 'Leafy Greens', icon: '🥬' }, { value: 'root_vegetables', label: 'Root Vegetables', icon: '🥕' }, { value: 'nightshades', label: 'Nightshades', icon: '🍅' }, { value: 'brassicas', label: 'Brassicas', icon: '🥦' }, { value: 'alliums', label: 'Alliums', icon: '🧅' }, { value: 'legumes', label: 'Legumes', icon: '🫘' }, { value: 'squash', label: 'Squash', icon: '🎃' }, { value: 'herbs', label: 'Herbs', icon: '🌿' }, { value: 'microgreens', label: 'Microgreens', icon: '🌱' }, { value: 'sprouts', label: 'Sprouts', icon: '🌾' }, { value: 'mushrooms', label: 'Mushrooms', icon: '🍄' }, { value: 'fruits', label: 'Fruits', icon: '🍎' }, { value: 'berries', label: 'Berries', icon: '🍓' }, { value: 'citrus', label: 'Citrus', icon: '🍊' }, { value: 'tree_fruits', label: 'Tree Fruits', icon: '🍑' }, { value: 'melons', label: 'Melons', icon: '🍈' }, { value: 'edible_flowers', label: 'Edible Flowers', icon: '🌸' }, ]; const CERTIFICATIONS = [ { value: 'organic', label: 'Organic' }, { value: 'non_gmo', label: 'Non-GMO' }, { value: 'biodynamic', label: 'Biodynamic' }, { value: 'local', label: 'Local' }, { value: 'heirloom', label: 'Heirloom' }, ] as const; const DELIVERY_METHODS = [ { value: 'home_delivery', label: 'Home Delivery' }, { value: 'pickup_point', label: 'Pickup Point' }, { value: 'farmers_market', label: 'Farmers Market' }, { value: 'csa', label: 'CSA Box' }, ] as const; const DELIVERY_FREQUENCIES = [ { value: 'daily', label: 'Daily' }, { value: 'twice_weekly', label: 'Twice Weekly' }, { value: 'weekly', label: 'Weekly' }, { value: 'bi_weekly', label: 'Bi-Weekly' }, { value: 'monthly', label: 'Monthly' }, ] as const; export default function PreferencesForm({ initialPreferences, onSubmit, loading = false, }: PreferencesFormProps) { const [activeSection, setActiveSection] = useState('dietary'); const [dietaryType, setDietaryType] = useState(initialPreferences?.dietaryType || ['omnivore']); const [allergies, setAllergies] = useState(initialPreferences?.allergies?.join(', ') || ''); const [dislikes, setDislikes] = useState(initialPreferences?.dislikes?.join(', ') || ''); const [preferredCategories, setPreferredCategories] = useState( initialPreferences?.preferredCategories || [] ); const [certifications, setCertifications] = useState( initialPreferences?.certificationPreferences || [] ); const [freshnessImportance, setFreshnessImportance] = useState( initialPreferences?.freshnessImportance || 4 ); const [priceImportance, setPriceImportance] = useState( initialPreferences?.priceImportance || 3 ); const [sustainabilityImportance, setSustainabilityImportance] = useState( initialPreferences?.sustainabilityImportance || 4 ); const [deliveryMethods, setDeliveryMethods] = useState( initialPreferences?.deliveryPreferences?.method || ['home_delivery'] ); const [deliveryFrequency, setDeliveryFrequency] = useState( initialPreferences?.deliveryPreferences?.frequency || 'weekly' ); const [householdSize, setHouseholdSize] = useState( initialPreferences?.householdSize || 2 ); const [weeklyBudget, setWeeklyBudget] = useState( initialPreferences?.weeklyBudget ); const [maxDeliveryRadius, setMaxDeliveryRadius] = useState( initialPreferences?.location?.maxDeliveryRadiusKm || 25 ); const toggleCategory = (category: ProduceCategory) => { setPreferredCategories((prev) => prev.includes(category) ? prev.filter((c) => c !== category) : [...prev, category] ); }; const toggleCertification = (cert: string) => { setCertifications((prev) => prev.includes(cert) ? prev.filter((c) => c !== cert) : [...prev, cert] ); }; const toggleDeliveryMethod = (method: string) => { setDeliveryMethods((prev) => prev.includes(method) ? prev.filter((m) => m !== method) : [...prev, method] ); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const preferences: Partial = { dietaryType: dietaryType as ConsumerPreference['dietaryType'], allergies: allergies.split(',').map((a) => a.trim()).filter(Boolean), dislikes: dislikes.split(',').map((d) => d.trim()).filter(Boolean), preferredCategories, certificationPreferences: certifications as ConsumerPreference['certificationPreferences'], freshnessImportance: freshnessImportance as 1 | 2 | 3 | 4 | 5, priceImportance: priceImportance as 1 | 2 | 3 | 4 | 5, sustainabilityImportance: sustainabilityImportance as 1 | 2 | 3 | 4 | 5, deliveryPreferences: { method: deliveryMethods as ConsumerPreference['deliveryPreferences']['method'], frequency: deliveryFrequency as ConsumerPreference['deliveryPreferences']['frequency'], preferredDays: ['saturday'], // Default }, householdSize, weeklyBudget, location: { latitude: 0, longitude: 0, maxDeliveryRadiusKm: maxDeliveryRadius, }, }; onSubmit(preferences); }; const sections = [ { id: 'dietary', name: 'Diet', icon: '🥗' }, { id: 'produce', name: 'Produce', icon: '🥬' }, { id: 'quality', name: 'Quality', icon: '⭐' }, { id: 'delivery', name: 'Delivery', icon: '📦' }, { id: 'household', name: 'Household', icon: '🏠' }, ]; const ImportanceSlider = ({ label, value, onChange, helpText, }: { label: string; value: number; onChange: (value: number) => void; helpText: string; }) => (
{value}/5
onChange(parseInt(e.target.value))} className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-green-600" />

{helpText}

); return (
{/* Section Tabs */}
{sections.map((section) => ( ))}
{/* Dietary Section */} {activeSection === 'dietary' && (

Dietary Preferences

{DIETARY_TYPES.map((type) => ( ))}
setAllergies(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500" placeholder="e.g., nuts, shellfish, gluten" />
setDislikes(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500" placeholder="e.g., cilantro, mushrooms" />
)} {/* Produce Section */} {activeSection === 'produce' && (

Produce Preferences

{PRODUCE_CATEGORIES.map((category) => ( ))}
)} {/* Quality Section */} {activeSection === 'quality' && (

Quality Preferences

{CERTIFICATIONS.map((cert) => ( ))}
)} {/* Delivery Section */} {activeSection === 'delivery' && (

Delivery Preferences

{DELIVERY_METHODS.map((method) => ( ))}
setMaxDeliveryRadius(parseInt(e.target.value))} className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-green-600" />

Shorter radius = fresher produce, fewer food miles

)} {/* Household Section */} {activeSection === 'household' && (

Household Info

setHouseholdSize(parseInt(e.target.value))} className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-green-600" />
$ setWeeklyBudget(e.target.value ? parseInt(e.target.value) : undefined)} className="w-full pl-8 pr-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500" placeholder="e.g., 100" />

Helps us recommend produce within your budget

)} {/* Submit Button */}
); }