CREATE TABLE graph_runs (
    id          BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id   BIGINT NOT NULL,
    project_id  BIGINT NOT NULL,
    goal        TEXT NOT NULL,
    status      VARCHAR(16) NOT NULL DEFAULT 'PENDING'
                    COMMENT 'PENDING|RUNNING|PAUSED|DONE|FAILED',
    created_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at  DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    INDEX idx_project (project_id),
    INDEX idx_tenant_project (tenant_id, project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE graph_steps (
    id           BIGINT NOT NULL AUTO_INCREMENT,
    run_id       BIGINT NOT NULL,
    step_key     VARCHAR(64) NOT NULL,
    step_name    VARCHAR(128) NOT NULL,
    agent_type   VARCHAR(16) NOT NULL COMMENT 'DECISION|EXECUTOR|SUPERVISION',
    status       VARCHAR(16) NOT NULL DEFAULT 'PENDING'
                     COMMENT 'PENDING|RUNNING|DONE|FAILED|SKIPPED',
    score        CHAR(1)  COMMENT 'A|B|C|D — supervision only',
    score_detail TEXT,
    output       TEXT,
    duration_ms  BIGINT,
    started_at   DATETIME(3),
    done_at      DATETIME(3),
    seq          INT NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    INDEX idx_run (run_id),
    UNIQUE KEY uk_run_step (run_id, step_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
