-- 002_event_participation_unique_constraints.sql (post-schema phase)
-- Composite UNIQUE constraints on the event-participation collections.
--
-- Same rationale as 001 in this dir: tables are Directus-managed (events,
-- classes, entries, entry_crew, entry_devices), created by schema-apply,
-- so the constraints land here in db-init-post/ rather than in db-init/.
--
-- All ALTER TABLE statements are wrapped in pg_constraint existence guards
-- for idempotency against pre-existing constraints (see 001 for full
-- rationale).

DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'events_org_slug_unique'
  ) THEN
    ALTER TABLE events
      ADD CONSTRAINT events_org_slug_unique
      UNIQUE (organization_id, slug);
  END IF;
END $$;

DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'classes_event_code_unique'
  ) THEN
    ALTER TABLE classes
      ADD CONSTRAINT classes_event_code_unique
      UNIQUE (event_id, code);
  END IF;
END $$;

DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'entries_event_race_number_unique'
  ) THEN
    ALTER TABLE entries
      ADD CONSTRAINT entries_event_race_number_unique
      UNIQUE (event_id, race_number);
  END IF;
END $$;

DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'entry_crew_entry_user_unique'
  ) THEN
    ALTER TABLE entry_crew
      ADD CONSTRAINT entry_crew_entry_user_unique
      UNIQUE (entry_id, user_id);
  END IF;
END $$;

DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'entry_devices_entry_device_unique'
  ) THEN
    ALTER TABLE entry_devices
      ADD CONSTRAINT entry_devices_entry_device_unique
      UNIQUE (entry_id, device_id);
  END IF;
END $$;

-- -------------------------------------------------------------------------
-- Assertion block: verify all five constraints landed.
-- -------------------------------------------------------------------------
DO $$ BEGIN

  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'events_org_slug_unique'
  ) THEN
    RAISE EXCEPTION 'events composite unique constraint (org, slug) missing';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'classes_event_code_unique'
  ) THEN
    RAISE EXCEPTION 'classes composite unique constraint (event, code) missing';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'entries_event_race_number_unique'
  ) THEN
    RAISE EXCEPTION 'entries composite unique constraint (event, race_number) missing';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'entry_crew_entry_user_unique'
  ) THEN
    RAISE EXCEPTION 'entry_crew composite unique constraint (entry, user) missing';
  END IF;

  IF NOT EXISTS (
    SELECT 1 FROM pg_constraint WHERE conname = 'entry_devices_entry_device_unique'
  ) THEN
    RAISE EXCEPTION 'entry_devices composite unique constraint (entry, device) missing';
  END IF;

END $$;
