-- sql/migrations/staging/005_subscription_status_cache.sql
-- Cache the current health state of each subscribed site so the customer
-- page can show SSL / DNS / WHOIS / HTTP status at a glance without
-- redoing the work on every page load. The scheduled scanner writes
-- these on every run, success or fail.

ALTER TABLE whp.ai_monitor_subscriptions
    ADD COLUMN IF NOT EXISTS http_status_code     SMALLINT NULL
        COMMENT 'Last observed HTTP status from the periodic probe.',
    ADD COLUMN IF NOT EXISTS http_checked_at      DATETIME NULL,

    ADD COLUMN IF NOT EXISTS ssl_status           ENUM('ok','expiring','expired','unreachable','unknown') NOT NULL DEFAULT 'unknown',
    ADD COLUMN IF NOT EXISTS ssl_expires_at       DATETIME NULL,
    ADD COLUMN IF NOT EXISTS ssl_issuer           VARCHAR(128) NULL,
    ADD COLUMN IF NOT EXISTS ssl_checked_at       DATETIME NULL,

    ADD COLUMN IF NOT EXISTS dns_status           ENUM('ok','mismatch','nxdomain','skipped','unknown') NOT NULL DEFAULT 'unknown',
    ADD COLUMN IF NOT EXISTS dns_observed_ips     VARCHAR(255) NULL
        COMMENT 'Comma-separated list of A records resolved at last check.',
    ADD COLUMN IF NOT EXISTS dns_checked_at       DATETIME NULL,

    ADD COLUMN IF NOT EXISTS whois_status         ENUM('ok','expiring','expired','unparsed','skipped','unknown') NOT NULL DEFAULT 'unknown',
    ADD COLUMN IF NOT EXISTS whois_expires_at     DATETIME NULL,
    ADD COLUMN IF NOT EXISTS whois_apex           VARCHAR(255) NULL,
    ADD COLUMN IF NOT EXISTS whois_checked_at     DATETIME NULL;
