-- Create new Supabase storage buckets for music and GIFs
-- Run this in your Supabase SQL Editor

-- 1. Create music bucket
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
    'music',
    'music',
    true,
    52428800, -- 50MB limit for music files
    ARRAY['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/mp4', 'audio/aac']
) ON CONFLICT (id) DO NOTHING;

-- 2. Create gifs bucket
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
    'gifs',
    'gifs',
    true,
    10485760, -- 10MB limit for GIF files
    ARRAY['image/gif', 'image/gifv']
) ON CONFLICT (id) DO NOTHING;

-- 3. Create storage policies for music bucket
CREATE POLICY "Public Access to Music" ON storage.objects
    FOR SELECT USING (bucket_id = 'music');

CREATE POLICY "Authenticated users can upload music" ON storage.objects
    FOR INSERT WITH CHECK (
        bucket_id = 'music' 
        AND auth.role() = 'authenticated'
    );

CREATE POLICY "Users can update own music" ON storage.objects
    FOR UPDATE USING (
        bucket_id = 'music' 
        AND auth.uid()::text = (storage.foldername(name))[1]
    );

CREATE POLICY "Users can delete own music" ON storage.objects
    FOR DELETE USING (
        bucket_id = 'music' 
        AND auth.uid()::text = (storage.foldername(name))[1]
    );

-- 4. Create storage policies for gifs bucket
CREATE POLICY "Public Access to GIFs" ON storage.objects
    FOR SELECT USING (bucket_id = 'gifs');

CREATE POLICY "Authenticated users can upload GIFs" ON storage.objects
    FOR INSERT WITH CHECK (
        bucket_id = 'gifs' 
        AND auth.role() = 'authenticated'
    );

CREATE POLICY "Users can update own GIFs" ON storage.objects
    FOR UPDATE USING (
        bucket_id = 'gifs' 
        AND auth.uid()::text = (storage.foldername(name))[1]
    );

CREATE POLICY "Users can delete own GIFs" ON storage.objects
    FOR DELETE USING (
        bucket_id = 'gifs' 
        AND auth.uid()::text = (storage.foldername(name))[1]
    );

-- 5. Test the setup
SELECT 'Music and GIFs buckets created successfully!' as status;

-- 6. Show bucket information
SELECT 
    id,
    name,
    public,
    file_size_limit,
    allowed_mime_types
FROM storage.buckets 
WHERE id IN ('music', 'gifs');
