-- Per-campaign banner colour overrides and the urgent (always-show) flag.
--
-- ad_campaigns gains two optional colour sets and an urgent flag, all for
-- non-default campaigns only:
--   override_bg/fg/link           — paints the strip on every theme
--   override_bg/fg/link_dark      — further overrides the dark theme
--   urgent                        — force the banner on every viewer (bypassing
--                                   eligibility) and suppress all non-urgent
--                                   campaigns while any urgent one is active
--
-- Each colour set is all-or-nothing (bg+fg+link together, or absent) and every
-- colour is a "#rrggbb" hex string. The default (house) campaign stays plain: no
-- colours, never urgent.
--
-- Expand-contract: additive (nullable columns plus one NOT NULL boolean with a
-- default), so a backend image rollback stays DB-safe — older code ignores them.
-- The ad_campaigns table shape changes, so its generated go-jet model is
-- regenerated (by hand here, to keep the diff to this one table).

-- +goose Up
ALTER TABLE backend.ad_campaigns
    ADD COLUMN override_bg text,
    ADD COLUMN override_fg text,
    ADD COLUMN override_link text,
    ADD COLUMN override_bg_dark text,
    ADD COLUMN override_fg_dark text,
    ADD COLUMN override_link_dark text,
    ADD COLUMN urgent boolean DEFAULT false NOT NULL,
    ADD CONSTRAINT ad_campaigns_override_all_chk CHECK (
        (override_bg IS NULL AND override_fg IS NULL AND override_link IS NULL)
        OR (override_bg IS NOT NULL AND override_fg IS NOT NULL AND override_link IS NOT NULL)
    ),
    ADD CONSTRAINT ad_campaigns_override_dark_chk CHECK (
        (override_bg_dark IS NULL AND override_fg_dark IS NULL AND override_link_dark IS NULL)
        OR (override_bg_dark IS NOT NULL AND override_fg_dark IS NOT NULL AND override_link_dark IS NOT NULL)
    ),
    ADD CONSTRAINT ad_campaigns_override_hex_chk CHECK (
        (override_bg IS NULL OR override_bg ~ '^#[0-9a-fA-F]{6}$')
        AND (override_fg IS NULL OR override_fg ~ '^#[0-9a-fA-F]{6}$')
        AND (override_link IS NULL OR override_link ~ '^#[0-9a-fA-F]{6}$')
        AND (override_bg_dark IS NULL OR override_bg_dark ~ '^#[0-9a-fA-F]{6}$')
        AND (override_fg_dark IS NULL OR override_fg_dark ~ '^#[0-9a-fA-F]{6}$')
        AND (override_link_dark IS NULL OR override_link_dark ~ '^#[0-9a-fA-F]{6}$')
    ),
    ADD CONSTRAINT ad_campaigns_default_plain_chk CHECK (
        (NOT is_default)
        OR (override_bg IS NULL AND override_fg IS NULL AND override_link IS NULL
            AND override_bg_dark IS NULL AND override_fg_dark IS NULL AND override_link_dark IS NULL
            AND NOT urgent)
    );

-- +goose Down
ALTER TABLE backend.ad_campaigns
    DROP COLUMN override_bg,
    DROP COLUMN override_fg,
    DROP COLUMN override_link,
    DROP COLUMN override_bg_dark,
    DROP COLUMN override_fg_dark,
    DROP COLUMN override_link_dark,
    DROP COLUMN urgent;
