{"-- Ensure model-files bucket exists and is public
INSERT INTO storage.buckets (id, name, public)
VALUES ('model-files', 'model-files', true)
ON CONFLICT (id) DO NOTHING","-- Policy: Public read access to model-files
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies WHERE policyname = 'Public can read model-files'
  ) THEN
    CREATE POLICY \"Public can read model-files\"
    ON storage.objects
    FOR SELECT
    USING (bucket_id = 'model-files');
  END IF;
END $$","-- Policy: Authenticated users can upload to model-files
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies WHERE policyname = 'Authenticated users can upload to model-files'
  ) THEN
    CREATE POLICY \"Authenticated users can upload to model-files\"
    ON storage.objects
    FOR INSERT
    TO authenticated
    WITH CHECK (bucket_id = 'model-files');
  END IF;
END $$","-- Policy: Authenticated users can update their uploads to model-files
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies WHERE policyname = 'Authenticated users can update model-files'
  ) THEN
    CREATE POLICY \"Authenticated users can update model-files\"
    ON storage.objects
    FOR UPDATE
    TO authenticated
    USING (bucket_id = 'model-files')
    WITH CHECK (bucket_id = 'model-files');
  END IF;
END $$","-- Policy: Authenticated users can delete their uploads to model-files
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies WHERE policyname = 'Authenticated users can delete model-files'
  ) THEN
    CREATE POLICY \"Authenticated users can delete model-files\"
    ON storage.objects
    FOR DELETE
    TO authenticated
    USING (bucket_id = 'model-files');
  END IF;
END $$"}