-- Create storage bucket for uploaded files
INSERT INTO storage.buckets (id, name, public, file_size_limit)
VALUES ('files', 'files', true, 52428800) -- 50MB max file size
ON CONFLICT (id) DO NOTHING;

-- Allow authenticated users to upload files (organized by org_id/folder)
CREATE POLICY "Authenticated users can upload files"
ON storage.objects FOR INSERT
TO authenticated
WITH CHECK (bucket_id = 'files');

-- Allow authenticated users to update their files
CREATE POLICY "Authenticated users can update files"
ON storage.objects FOR UPDATE
TO authenticated
USING (bucket_id = 'files');

-- Allow public read access to all files
CREATE POLICY "Public read access for files"
ON storage.objects FOR SELECT
TO public
USING (bucket_id = 'files');

-- Allow authenticated users to delete files
CREATE POLICY "Authenticated users can delete files"
ON storage.objects FOR DELETE
TO authenticated
USING (bucket_id = 'files');
