-- Migration 116: Banner A/B Testing
-- Enables variant testing for consent rate optimization

CREATE TABLE IF NOT EXISTS compliance_banner_variants (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL,
    site_config_id UUID NOT NULL,
    variant_name VARCHAR(100) NOT NULL,
    variant_key VARCHAR(20) NOT NULL,
    traffic_percent INT NOT NULL DEFAULT 50 CHECK (traffic_percent BETWEEN 0 AND 100),
    is_control BOOLEAN NOT NULL DEFAULT FALSE,
    banner_title TEXT,
    banner_description TEXT,
    position VARCHAR(20),
    style VARCHAR(20),
    primary_color VARCHAR(20),
    show_decline_all BOOLEAN,
    theme_overrides JSONB DEFAULT '{}',
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE(site_config_id, variant_key)
);

CREATE INDEX IF NOT EXISTS idx_banner_variants_site
    ON compliance_banner_variants(site_config_id);

ALTER TABLE compliance_banner_consent_audit_log
    ADD COLUMN IF NOT EXISTS variant_id UUID;
ALTER TABLE compliance_banner_consent_audit_log
    ADD COLUMN IF NOT EXISTS variant_key VARCHAR(20);
