'use server';

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

export async function updateBottleStatus(bottleId: string, status: 'sealed' | 'open' | 'sampled' | 'empty') {
    const supabase = createServerActionClient({ cookies });

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

        const { error } = await supabase
            .from('bottles')
            .update({ status, updated_at: new Date().toISOString() })
            .eq('id', bottleId)
            .eq('user_id', session.user.id);

        if (error) throw error;

        revalidatePath(`/bottles/${bottleId}`);
        revalidatePath('/');

        return { success: true };
    } catch (error) {
        console.error('Update Status Error:', error);
        return {
            success: false,
            error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren des Status',
        };
    }
}
