-- Additive: offer_type on offer_macros (una_tantum | retainer) + dedup guard.
-- The UNIQUE constraint must be added AFTER the one-time dedup of duplicate
-- offer_micros (same macro_id + tier_letter), otherwise it fails on existing dupes.
-- NULLS NOT DISTINCT treats two NULL tier_letter rows under the same macro as a
-- conflict too; if that is undesired for legacy non-tier micros, switch to the
-- partial-index variant below. No drops/truncates.

ALTER TABLE offer_macros ADD COLUMN IF NOT EXISTS offer_type text NOT NULL DEFAULT 'una_tantum';

-- Run ONLY after dedup. Prevents future duplicate tiers within an offer.
ALTER TABLE offer_micros
  ADD CONSTRAINT offer_micros_macro_tier_unique UNIQUE NULLS NOT DISTINCT (macro_id, tier_letter);

-- Alternative (allows multiple NULL-tier micros per macro):
-- CREATE UNIQUE INDEX offer_micros_macro_tier_unique
--   ON offer_micros (macro_id, tier_letter) WHERE tier_letter IS NOT NULL;
