CREATE TABLE IF NOT EXISTS system_error_log (
    id BIGSERIAL PRIMARY KEY,
    created_at TEXT NOT NULL,
    level TEXT DEFAULT 'error',
    source TEXT DEFAULT 'app',
    error_type TEXT DEFAULT '',
    message TEXT DEFAULT '',
    stack_trace TEXT DEFAULT '',
    request_method TEXT DEFAULT '',
    request_path TEXT DEFAULT '',
    query_string TEXT DEFAULT '',
    user_email TEXT DEFAULT '',
    user_id BIGINT DEFAULT 0,
    status_code INTEGER DEFAULT 0,
    fingerprint TEXT DEFAULT '',
    context_json TEXT DEFAULT '{}',
    host TEXT DEFAULT '',
    pid INTEGER DEFAULT 0,
    resolved_at TEXT DEFAULT '',
    resolved_by TEXT DEFAULT '',
    resolution_note TEXT DEFAULT ''
);

CREATE INDEX IF NOT EXISTS idx_system_error_log_created ON system_error_log(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_system_error_log_level_source ON system_error_log(level, source, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_system_error_log_fingerprint ON system_error_log(fingerprint, created_at DESC);
