-- Migration 002: Add contract_url column + create storage buckets for contracts
-- Schema: natur_reservas
-- Run this in the Supabase SQL editor

-- ── 1. Add contract_url column ────────────────────────────────────────────────
ALTER TABLE natur_reservas.reservations
ADD COLUMN IF NOT EXISTS contract_url text;

-- ── 2. Create storage buckets (one per property) ─────────────────────────────
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES
  (
    'contratos-los-dragos',
    'contratos-los-dragos',
    true,
    10485760,  -- 10 MB
    ARRAY['application/pdf', 'image/jpeg', 'image/png']
  ),
  (
    'contratos-la-esquinita',
    'contratos-la-esquinita',
    true,
    10485760,
    ARRAY['application/pdf', 'image/jpeg', 'image/png']
  )
ON CONFLICT (id) DO NOTHING;

-- ── 3. Storage RLS policies ───────────────────────────────────────────────────

-- contratos-los-dragos
CREATE POLICY "contratos-los-dragos: select"
  ON storage.objects FOR SELECT TO anon, authenticated
  USING (bucket_id = 'contratos-los-dragos');

CREATE POLICY "contratos-los-dragos: insert"
  ON storage.objects FOR INSERT TO anon, authenticated
  WITH CHECK (bucket_id = 'contratos-los-dragos');

CREATE POLICY "contratos-los-dragos: update"
  ON storage.objects FOR UPDATE TO anon, authenticated
  USING (bucket_id = 'contratos-los-dragos');

CREATE POLICY "contratos-los-dragos: delete"
  ON storage.objects FOR DELETE TO anon, authenticated
  USING (bucket_id = 'contratos-los-dragos');

-- contratos-la-esquinita
CREATE POLICY "contratos-la-esquinita: select"
  ON storage.objects FOR SELECT TO anon, authenticated
  USING (bucket_id = 'contratos-la-esquinita');

CREATE POLICY "contratos-la-esquinita: insert"
  ON storage.objects FOR INSERT TO anon, authenticated
  WITH CHECK (bucket_id = 'contratos-la-esquinita');

CREATE POLICY "contratos-la-esquinita: update"
  ON storage.objects FOR UPDATE TO anon, authenticated
  USING (bucket_id = 'contratos-la-esquinita');

CREATE POLICY "contratos-la-esquinita: delete"
  ON storage.objects FOR DELETE TO anon, authenticated
  USING (bucket_id = 'contratos-la-esquinita');
