'use server';

import { createClient } from '@/lib/supabase/server';
import { revalidatePath } from 'next/cache';

export async function deleteTasting(tastingId: string, bottleId: string) {
    const supabase = await createClient();

    try {
        const { data: { user } } = await supabase.auth.getUser();
        if (!user) {
            throw new Error('Nicht autorisiert.');
        }

        const { error: deleteError } = await supabase
            .from('tastings')
            .delete()
            .eq('id', tastingId)
            .eq('user_id', user.id);

        if (deleteError) throw deleteError;

        revalidatePath(`/bottles/${bottleId}`);
        return { success: true };
    } catch (error) {
        console.error('Delete Tasting Error:', error);
        return {
            success: false,
            error: error instanceof Error ? error.message : 'Fehler beim Löschen.',
        };
    }
}
