'use server';

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

export async function deleteSession(sessionId: 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('tasting_sessions')
            .delete()
            .eq('id', sessionId)
            .eq('user_id', user.id);

        if (deleteError) throw deleteError;

        revalidatePath('/');
        revalidatePath(`/sessions/${sessionId}`);

        return { success: true };
    } catch (error) {
        console.error('Delete Session Error:', error);
        return {
            success: false,
            error: error instanceof Error ? error.message : 'Fehler beim Löschen der Session.',
        };
    }
}
