-- ============================================
-- Enable Public Access for Bottle Splits
-- ============================================
-- This script enables unauthenticated users (guests) to view:
-- 1. Profiles (to see hostnames)
-- 2. Bottle Splits (if is_active = true)
-- 3. Split Participants (to see progress bar data)
-- ============================================

-- 1. Profiles: Allow anyone to see usernames and avatars
DROP POLICY IF EXISTS "profiles_select_policy" ON profiles;
CREATE POLICY "profiles_select_policy" ON profiles
  FOR SELECT USING (true);

-- 2. Bottle Splits: Allow guests to see active splits
-- This policy allows anyone to read splits that are marked as active.
DROP POLICY IF EXISTS "bottle_splits_public_select" ON bottle_splits;
CREATE POLICY "bottle_splits_public_select" ON bottle_splits
  FOR SELECT USING (is_active = true);

-- 3. Split Participants: Allow guests to see progress of active splits
-- This is necessary to calculate the "taken" volume in the progress bar.
DROP POLICY IF EXISTS "split_participants_public_select" ON split_participants;
CREATE POLICY "split_participants_public_select" ON split_participants
  FOR SELECT USING (
    EXISTS (
      SELECT 1 FROM public.bottle_splits 
      WHERE id = split_participants.split_id AND is_active = true
    )
  );

-- Note: Ensure "bottles_select_policy" in rls_public_bottle_access.sql 
-- is also applied to allow guests to see bottle details for active splits.
