-- RAIET v1.44.0 — internal diagnostics / system health.
CREATE TABLE IF NOT EXISTS system_events (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    component VARCHAR(40) NOT NULL,
    event_type VARCHAR(80) NOT NULL,
    severity ENUM('info','warning','error') NOT NULL DEFAULT 'info',
    user_id BIGINT UNSIGNED NULL,
    lobby_id BIGINT UNSIGNED NULL,
    detail VARCHAR(255) NOT NULL DEFAULT '',
    context_json TEXT NULL,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (id),
    KEY idx_system_events_created (created_at),
    KEY idx_system_events_severity (severity, created_at),
    KEY idx_system_events_component (component, created_at),
    KEY idx_system_events_lobby (lobby_id, created_at),
    CONSTRAINT fk_system_events_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL,
    CONSTRAINT fk_system_events_lobby FOREIGN KEY (lobby_id) REFERENCES lobbies(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
