-- Add public_slug column to trips table
ALTER TABLE trips
ADD COLUMN IF NOT EXISTS public_slug TEXT UNIQUE;

-- Create index for faster lookups
CREATE INDEX IF NOT EXISTS idx_trips_public_slug ON trips(public_slug);

-- Add RLS policy for public access to trips with public_slug
CREATE POLICY "Public trips are viewable by anyone"
ON trips FOR SELECT
USING (is_public = true AND public_slug IS NOT NULL);

-- Add RLS policy for public access to trip_days
CREATE POLICY "Public trip days are viewable by anyone"
ON trip_days FOR SELECT
USING (
  EXISTS (
    SELECT 1 FROM trips
    WHERE trips.id = trip_days.trip_id
    AND trips.is_public = true
    AND trips.public_slug IS NOT NULL
  )
);

-- Add RLS policy for public access to trip_places
CREATE POLICY "Public trip places are viewable by anyone"
ON trip_places FOR SELECT
USING (
  EXISTS (
    SELECT 1 FROM trip_days
    JOIN trips ON trips.id = trip_days.trip_id
    WHERE trip_days.id = trip_places.trip_day_id
    AND trips.is_public = true
    AND trips.public_slug IS NOT NULL
  )
);