'use client'; import React, { useState } from 'react'; import { Edit2, Save, X, Info, Tag, FlaskConical, CircleDollarSign, Search, Loader2, ExternalLink } from 'lucide-react'; import { updateBottle } from '@/services/update-bottle'; import { discoverWhiskybaseId } from '@/services/discover-whiskybase'; import { useI18n } from '@/i18n/I18nContext'; interface EditBottleFormProps { bottle: { id: string; name: string; distillery: string; category: string; abv: number; age: number; whiskybase_id: string | null; purchase_price?: number | null; distilled_at?: string | null; bottled_at?: string | null; batch_info?: string | null; cask_type?: string | null; }; onComplete?: () => void; } export default function EditBottleForm({ bottle, onComplete }: EditBottleFormProps) { const { t, locale } = useI18n(); const [isEditing, setIsEditing] = useState(false); const [isSaving, setIsSaving] = useState(false); const [isSearching, setIsSearching] = useState(false); const [error, setError] = useState(null); const [discoveryResult, setDiscoveryResult] = useState<{ id: string; url: string; title: string } | null>(null); const [formData, setFormData] = useState({ name: bottle.name, distillery: bottle.distillery || '', category: bottle.category || '', abv: bottle.abv?.toString() || '', age: bottle.age?.toString() || '', whiskybase_id: bottle.whiskybase_id || '', purchase_price: bottle.purchase_price?.toString() || '', distilled_at: bottle.distilled_at || '', bottled_at: bottle.bottled_at || '', batch_info: bottle.batch_info || '', cask_type: bottle.cask_type || '', }); const handleDiscover = async () => { setIsSearching(true); setError(null); setDiscoveryResult(null); const result = await discoverWhiskybaseId({ name: formData.name, distillery: formData.distillery, abv: formData.abv ? parseFloat(formData.abv) : undefined, age: formData.age ? parseInt(formData.age) : undefined, distilled_at: formData.distilled_at || undefined, bottled_at: formData.bottled_at || undefined, batch_info: formData.batch_info || undefined, }); if (result.success && result.id) { setDiscoveryResult({ id: result.id!, url: result.url!, title: result.title! }); } else { setError(result.error || t('bottle.noMatchFound')); } setIsSearching(false); }; const applyDiscovery = () => { if (discoveryResult) { setFormData({ ...formData, whiskybase_id: discoveryResult.id }); setDiscoveryResult(null); } }; const handleSave = async () => { setIsSaving(true); setError(null); try { const response = await updateBottle(bottle.id, { ...formData, abv: formData.abv ? parseFloat(formData.abv.replace(',', '.')) : null, age: formData.age ? parseInt(formData.age) : null, purchase_price: formData.purchase_price ? parseFloat(formData.purchase_price.replace(',', '.')) : null, distilled_at: formData.distilled_at || undefined, bottled_at: formData.bottled_at || undefined, batch_info: formData.batch_info || undefined, cask_type: formData.cask_type || undefined, } as any); if (response.success) { setIsEditing(false); if (onComplete) onComplete(); } else { setError(response.error || t('common.error')); } } catch (err) { setError(t('common.error')); } finally { setIsSaving(false); } }; return (
{/* Full Width Inputs */}
setFormData({ ...formData, name: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
setFormData({ ...formData, distillery: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
{/* Compact Row: Category */}
setFormData({ ...formData, category: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
{/* Row A: ABV + Age */}
setFormData({ ...formData, abv: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-orange-500 text-sm font-bold transition-all" placeholder="e.g. 46.3" />
setFormData({ ...formData, age: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all" placeholder="e.g. 12" />
{/* Row B: Distilled + Bottled */}
setFormData({ ...formData, distilled_at: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
setFormData({ ...formData, bottled_at: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
{/* Price and WB ID Row */}
setFormData({ ...formData, purchase_price: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 font-bold text-orange-500 text-sm transition-all" />
setFormData({ ...formData, whiskybase_id: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-300 text-sm font-mono transition-all" /> {discoveryResult && (

Recommendation:

{discoveryResult.title}

)}
{/* Batch and Cask */}
setFormData({ ...formData, batch_info: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
setFormData({ ...formData, cask_type: e.target.value })} className="w-full px-5 py-4 bg-black/40 border border-white/5 rounded-2xl outline-hidden focus:ring-2 focus:ring-orange-600/50 text-zinc-100 text-sm font-bold transition-all placeholder:text-zinc-700" />
{error && (
{error}
)}
); }