-- Storage RLS Policies for Trainer Assets
-- Run this in your Supabase SQL Editor

-- Enable RLS on storage buckets (if not already enabled)
ALTER TABLE storage.buckets ENABLE ROW LEVEL SECURITY;

-- Trainer Avatars Bucket Policies
DROP POLICY IF EXISTS "Public read access" ON storage.objects;
DROP POLICY IF EXISTS "Public upload access" ON storage.objects;
DROP POLICY IF EXISTS "Public update access" ON storage.objects;
DROP POLICY IF EXISTS "Public delete access" ON storage.objects;

-- Read access (anyone can view)
CREATE POLICY "Public read access" ON storage.objects
  FOR SELECT USING (bucket_id = 'trainer-avatars');

-- Upload access (anyone can upload)
CREATE POLICY "Public upload access" ON storage.objects
  FOR INSERT WITH CHECK (bucket_id = 'trainer-avatars');

-- Update access (anyone can update)
CREATE POLICY "Public update access" ON storage.objects
  FOR UPDATE USING (bucket_id = 'trainer-avatars');

-- Delete access (anyone can delete)
CREATE POLICY "Public delete access" ON storage.objects
  FOR DELETE USING (bucket_id = 'trainer-avatars');

-- Trainer Videos Bucket Policies
DROP POLICY IF EXISTS "Public read access videos" ON storage.objects;
DROP POLICY IF EXISTS "Public upload access videos" ON storage.objects;
DROP POLICY IF EXISTS "Public update access videos" ON storage.objects;
DROP POLICY IF EXISTS "Public delete access videos" ON storage.objects;

-- Read access (anyone can view)
CREATE POLICY "Public read access videos" ON storage.objects
  FOR SELECT USING (bucket_id = 'trainer-videos');

-- Upload access (anyone can upload)
CREATE POLICY "Public upload access videos" ON storage.objects
  FOR INSERT WITH CHECK (bucket_id = 'trainer-videos');

-- Update access (anyone can update)
CREATE POLICY "Public update access videos" ON storage.objects
  FOR UPDATE USING (bucket_id = 'trainer-videos');

-- Delete access (anyone can delete)
CREATE POLICY "Public delete access videos" ON storage.objects
  FOR DELETE USING (bucket_id = 'trainer-videos');

-- Exercise Videos Table Policies (if table exists)
DROP POLICY IF EXISTS "Public read access exercise_videos" ON exercise_videos;
DROP POLICY IF EXISTS "Public insert access exercise_videos" ON exercise_videos;
DROP POLICY IF EXISTS "Public update access exercise_videos" ON exercise_videos;
DROP POLICY IF EXISTS "Public delete access exercise_videos" ON exercise_videos;

CREATE POLICY "Public read access exercise_videos" ON exercise_videos FOR SELECT USING (true);
CREATE POLICY "Public insert access exercise_videos" ON exercise_videos FOR INSERT WITH CHECK (true);
CREATE POLICY "Public update access exercise_videos" ON exercise_videos FOR UPDATE USING (true);
CREATE POLICY "Public delete access exercise_videos" ON exercise_videos FOR DELETE USING (true);
