-- sql/migrations/staging/001_unify_ai_monitor_issues.sql
-- Unify ai_monitor_issues into a single tracker for both site-linked and
-- server-wide findings, and add signature-based suppression support.
-- See docs/superpowers/specs/2026-04-23-whp-ai-monitor-enhancements-design.md

-- The prior migration (sql/migrations/2026.04.34/001_customer_ai_monitoring.sql)
-- defined subscription_id/site_id/username as NOT NULL. This migration relaxes
-- those to NULL so server-wide Issues can exist. Columns stay signed to preserve
-- FK type compatibility with ai_monitor_subscriptions.id and sites.id (both INT
-- signed). The existing ON DELETE CASCADE FK on site_id already matches the
-- design's intent, so no FK add is needed here.
--
-- Hard cutover: drop is_read, is_resolved, resolved_at. No production issue
-- data to migrate at this point (confirmed with stakeholder 2026-04-23).

-- Relax NOT NULL on existing columns (idempotent: re-running with columns
-- already NULL is a no-op).
ALTER TABLE whp.ai_monitor_issues
    MODIFY subscription_id INT NULL,
    MODIFY site_id INT NULL,
    MODIFY username VARCHAR(32) NULL;

-- Add new columns (IF NOT EXISTS guards re-runs).
ALTER TABLE whp.ai_monitor_issues
    ADD COLUMN IF NOT EXISTS subsystem VARCHAR(32) NULL AFTER category,
    ADD COLUMN IF NOT EXISTS source VARCHAR(32) NOT NULL DEFAULT 'logs' AFTER subsystem,
    ADD COLUMN IF NOT EXISTS sources JSON NULL AFTER source,
    ADD COLUMN IF NOT EXISTS context_data JSON NULL AFTER raw_log_excerpt,
    ADD COLUMN IF NOT EXISTS status ENUM('open','fixed','ignored') NOT NULL DEFAULT 'open',
    ADD COLUMN IF NOT EXISTS auto_resolved_at DATETIME NULL,
    ADD COLUMN IF NOT EXISTS status_changed_by VARCHAR(32) NULL,
    ADD COLUMN IF NOT EXISTS status_changed_at DATETIME NULL;

-- Drop legacy columns (guarded by IF EXISTS for re-runnability).
ALTER TABLE whp.ai_monitor_issues
    DROP COLUMN IF EXISTS is_read,
    DROP COLUMN IF EXISTS is_resolved,
    DROP COLUMN IF EXISTS resolved_at;

-- New indexes (MySQL 8 supports IF NOT EXISTS on ADD INDEX).
ALTER TABLE whp.ai_monitor_issues
    ADD INDEX IF NOT EXISTS idx_site_status (site_id, status),
    ADD INDEX IF NOT EXISTS idx_subsystem_status (subsystem, status),
    ADD INDEX IF NOT EXISTS idx_source (source);

-- Suppressions table
CREATE TABLE IF NOT EXISTS whp.ai_monitor_suppressions (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    site_id INT NULL,
    signature VARCHAR(64) NOT NULL,
    scope ENUM('site','server') NOT NULL,
    suppressed_by_user VARCHAR(32) NOT NULL,
    suppressed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    reason TEXT NULL,
    suppressed_occurrence_count INT UNSIGNED NOT NULL DEFAULT 0,
    last_suppressed_event_at DATETIME NULL,
    UNIQUE KEY unique_suppression (site_id, signature),
    INDEX idx_signature (signature),
    CONSTRAINT fk_aim_supp_site FOREIGN KEY (site_id) REFERENCES whp.sites(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
