'use server';

import { createServerActionClient } from '@supabase/auth-helpers-nextjs';
import { cookies } from 'next/headers';
import { revalidatePath } from 'next/cache';

export async function deleteTasting(tastingId: string, bottleId: string) {
    const supabase = createServerActionClient({ cookies });

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

        const { error: deleteError } = await supabase
            .from('tastings')
            .delete()
            .eq('id', tastingId)
            .eq('user_id', session.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.',
        };
    }
}
