'use server';

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

/**
 * Validates if a session is still "active" based on its age.
 * Returns true if the session is less than 12 hours old.
 */
export async function validateSession(sessionId: string | null): Promise<boolean> {
    if (!sessionId) return false;

    const supabase = await createClient();

    try {
        const { data: session, error } = await supabase
            .from('tasting_sessions')
            .select('created_at')
            .eq('id', sessionId)
            .single();

        if (error || !session) return false;

        const createdAt = new Date(session.created_at).getTime();
        const now = new Date().getTime();
        const twelveHoursInMs = 12 * 60 * 60 * 1000;

        return (now - createdAt) < twelveHoursInMs;
    } catch (err) {
        console.error('Session validation error:', err);
        return false;
    }
}
