SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS structured_log_events (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    workspace_id BIGINT UNSIGNED NULL,
    user_id BIGINT UNSIGNED NULL,
    level ENUM('debug','info','warning','error','critical') NOT NULL DEFAULT 'info',
    message VARCHAR(255) NOT NULL,
    context JSON NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_log_ws_time(workspace_id,created_at),
    INDEX idx_log_level_time(level,created_at),
    CONSTRAINT fk_sle_workspace FOREIGN KEY(workspace_id) REFERENCES workspaces(id) ON DELETE SET NULL,
    CONSTRAINT fk_sle_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE notifications ADD COLUMN IF NOT EXISTS read_at DATETIME NULL;
