/*
  # Create storage bucket for blog media

  1. Storage Bucket
    - Create 'blog-media' bucket for images, audio, and video files
    - Set up public access policies

  2. Security
    - Public read access for all files
    - Authenticated write access for admin
*/

-- Create storage bucket for blog media
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
  'blog-media',
  'blog-media',
  true,
  52428800, -- 50MB limit
  ARRAY[
    'image/jpeg',
    'image/png',
    'image/webp',
    'image/gif',
    'audio/mpeg',
    'audio/wav',
    'audio/ogg',
    'video/mp4',
    'video/webm',
    'video/ogg',
    'application/pdf'
  ]
)
ON CONFLICT (id) DO NOTHING;

-- Create policy for public read access
CREATE POLICY "Public read access for blog media"
ON storage.objects
FOR SELECT
TO public
USING (bucket_id = 'blog-media');

-- Create policy for authenticated upload access
CREATE POLICY "Authenticated users can upload blog media"
ON storage.objects
FOR INSERT
TO authenticated
WITH CHECK (bucket_id = 'blog-media');

-- Create policy for authenticated update access
CREATE POLICY "Authenticated users can update blog media"
ON storage.objects
FOR UPDATE
TO authenticated
USING (bucket_id = 'blog-media')
WITH CHECK (bucket_id = 'blog-media');

-- Create policy for authenticated delete access
CREATE POLICY "Authenticated users can delete blog media"
ON storage.objects
FOR DELETE
TO authenticated
USING (bucket_id = 'blog-media');