ALTER TABLE public.amenity_bookings
  ADD COLUMN IF NOT EXISTS is_public_event boolean NOT NULL DEFAULT false,
  ADD COLUMN IF NOT EXISTS event_title text,
  ADD COLUMN IF NOT EXISTS event_description text,
  ADD COLUMN IF NOT EXISTS payment_status text NOT NULL DEFAULT 'none',
  ADD COLUMN IF NOT EXISTS payment_amount_cents integer NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS payment_link_url text,
  ADD COLUMN IF NOT EXISTS payment_paid_at timestamptz,
  ADD COLUMN IF NOT EXISTS signed_documents jsonb NOT NULL DEFAULT '[]'::jsonb;

ALTER TABLE public.amenities
  ADD COLUMN IF NOT EXISTS signable_documents jsonb NOT NULL DEFAULT '[]'::jsonb;

DROP FUNCTION IF EXISTS public.get_public_amenity_booked_dates(uuid);

CREATE FUNCTION public.get_public_amenity_booked_dates(p_amenity_id uuid)
RETURNS TABLE(booking_date date, start_time text, end_time text, status text, is_public_event boolean, event_title text)
LANGUAGE sql
STABLE SECURITY DEFINER
SET search_path TO 'public'
AS $$
  SELECT ab.booking_date, ab.start_time, ab.end_time, ab.status,
         COALESCE(ab.is_public_event, false) AS is_public_event,
         CASE WHEN COALESCE(ab.is_public_event, false) THEN COALESCE(ab.event_title, ab.title) ELSE NULL END AS event_title
  FROM public.amenity_bookings ab
  JOIN public.amenities a ON a.id = ab.amenity_id
  JOIN public.association_public_pages app ON app.association_id = a.association_id
  WHERE ab.amenity_id = p_amenity_id
    AND a.is_active = true
    AND app.is_published = true
    AND ab.status IN ('confirmed', 'approved', 'blocked')
    AND ab.booking_date >= (CURRENT_DATE - INTERVAL '7 days')
$$;

INSERT INTO storage.buckets (id, name, public)
VALUES ('amenity-documents', 'amenity-documents', true)
ON CONFLICT (id) DO NOTHING;

DO $$ BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='storage' AND tablename='objects' AND policyname='Public read amenity-documents') THEN
    CREATE POLICY "Public read amenity-documents" ON storage.objects FOR SELECT
      USING (bucket_id = 'amenity-documents');
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='storage' AND tablename='objects' AND policyname='Staff manage amenity-documents') THEN
    CREATE POLICY "Staff manage amenity-documents" ON storage.objects FOR ALL TO authenticated
      USING (bucket_id = 'amenity-documents' AND (public.has_role(auth.uid(),'admin'::public.app_role) OR public.has_role(auth.uid(),'manager'::public.app_role) OR public.has_role(auth.uid(),'employee'::public.app_role)))
      WITH CHECK (bucket_id = 'amenity-documents' AND (public.has_role(auth.uid(),'admin'::public.app_role) OR public.has_role(auth.uid(),'manager'::public.app_role) OR public.has_role(auth.uid(),'employee'::public.app_role)));
  END IF;
  IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='storage' AND tablename='objects' AND policyname='Anyone upload signed amenity-documents') THEN
    CREATE POLICY "Anyone upload signed amenity-documents" ON storage.objects FOR INSERT TO anon, authenticated
      WITH CHECK (bucket_id = 'amenity-documents' AND (storage.foldername(name))[1] = 'signed');
  END IF;
END $$;