-- ================================================
-- CUSTOMER AI MONITORING ADDON TABLES
-- ================================================
-- Adds per-site AI monitoring as a paid addon feature.
-- Customers purchase monitoring through WHMCS for individual sites.

-- Tracks which sites have paid AI monitoring enabled.
-- Source of truth for the Python daemon to know which sites to monitor.
CREATE TABLE IF NOT EXISTS whp.ai_monitor_subscriptions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    site_id INT NOT NULL,
    username VARCHAR(32) NOT NULL,
    notification_email VARCHAR(255) DEFAULT NULL COMMENT 'Email for issue notifications',
    daily_review_limit INT NOT NULL DEFAULT 3 COMMENT 'Max on-demand log reviews per day',
    status ENUM('active', 'suspended', 'cancelled') NOT NULL DEFAULT 'active',
    activated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    cancelled_at TIMESTAMP NULL DEFAULT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY unique_site_sub (site_id),
    INDEX idx_username (username),
    INDEX idx_status (status),
    FOREIGN KEY (site_id) REFERENCES whp.sites(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Customer-visible issues detected by the AI monitoring daemon.
-- Populated by the Python daemon when it finds issues for subscribed sites.
CREATE TABLE IF NOT EXISTS whp.ai_monitor_issues (
    id INT AUTO_INCREMENT PRIMARY KEY,
    subscription_id INT NOT NULL,
    site_id INT NOT NULL,
    username VARCHAR(32) NOT NULL,
    container_name VARCHAR(100) DEFAULT NULL COMMENT 'Specific container e.g. domain.tld-01',
    severity ENUM('critical', 'warning', 'info') NOT NULL DEFAULT 'warning',
    category VARCHAR(50) NOT NULL COMMENT 'oom, php_error, performance, security, resource_limit, crash',
    title VARCHAR(255) NOT NULL,
    diagnosis TEXT NOT NULL,
    recommendation TEXT NOT NULL,
    raw_log_excerpt TEXT DEFAULT NULL COMMENT 'Sanitized log excerpt (server paths stripped)',
    signature VARCHAR(64) NOT NULL COMMENT 'SHA-256 hash for deduplication',
    occurrence_count INT NOT NULL DEFAULT 1,
    is_read TINYINT(1) NOT NULL DEFAULT 0,
    is_resolved TINYINT(1) NOT NULL DEFAULT 0,
    resolved_at TIMESTAMP NULL DEFAULT NULL,
    email_sent TINYINT(1) NOT NULL DEFAULT 0,
    email_sent_at TIMESTAMP NULL DEFAULT NULL,
    first_seen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_seen TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_subscription_id (subscription_id),
    INDEX idx_site_username (site_id, username),
    INDEX idx_severity (severity),
    INDEX idx_email_sent (email_sent),
    INDEX idx_created_at (created_at),
    INDEX idx_signature (signature),
    INDEX idx_is_resolved (is_resolved),
    FOREIGN KEY (subscription_id) REFERENCES whp.ai_monitor_subscriptions(id) ON DELETE CASCADE,
    FOREIGN KEY (site_id) REFERENCES whp.sites(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- On-demand log review requests from customers.
-- Rate-limited by daily_review_limit in ai_monitor_subscriptions.
CREATE TABLE IF NOT EXISTS whp.ai_monitor_log_reviews (
    id INT AUTO_INCREMENT PRIMARY KEY,
    subscription_id INT NOT NULL,
    site_id INT NOT NULL,
    username VARCHAR(32) NOT NULL,
    status ENUM('pending', 'processing', 'completed', 'failed') NOT NULL DEFAULT 'pending',
    request_type ENUM('full', 'error_only', 'performance') NOT NULL DEFAULT 'full',
    ai_model_used VARCHAR(50) DEFAULT NULL,
    ai_tokens_used INT NOT NULL DEFAULT 0,
    result_summary TEXT DEFAULT NULL,
    result_details JSON DEFAULT NULL COMMENT 'Structured JSON with findings',
    error_message TEXT DEFAULT NULL,
    requested_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP NULL DEFAULT NULL,
    INDEX idx_subscription_id (subscription_id),
    INDEX idx_username_date (username, requested_at),
    INDEX idx_status (status),
    FOREIGN KEY (subscription_id) REFERENCES whp.ai_monitor_subscriptions(id) ON DELETE CASCADE,
    FOREIGN KEY (site_id) REFERENCES whp.sites(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Email notification queue for customer AI monitoring alerts.
-- Processed by a cron job that sends via SMTP.
CREATE TABLE IF NOT EXISTS whp.ai_monitor_email_queue (
    id INT AUTO_INCREMENT PRIMARY KEY,
    subscription_id INT NOT NULL,
    issue_id INT DEFAULT NULL COMMENT 'NULL for review-complete emails',
    review_id INT DEFAULT NULL COMMENT 'NULL for issue notification emails',
    recipient_email VARCHAR(255) NOT NULL,
    subject VARCHAR(255) NOT NULL,
    body_html TEXT NOT NULL,
    body_text TEXT NOT NULL,
    status ENUM('queued', 'sending', 'sent', 'failed') NOT NULL DEFAULT 'queued',
    attempts INT NOT NULL DEFAULT 0,
    max_attempts INT NOT NULL DEFAULT 3,
    last_attempt_at TIMESTAMP NULL DEFAULT NULL,
    sent_at TIMESTAMP NULL DEFAULT NULL,
    error_message TEXT DEFAULT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_status (status),
    INDEX idx_created_at (created_at),
    INDEX idx_subscription_batch (subscription_id, status, created_at),
    FOREIGN KEY (subscription_id) REFERENCES whp.ai_monitor_subscriptions(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
