CREATE TABLE script_understandings (
    id                   BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id            BIGINT NOT NULL,
    project_id           BIGINT NOT NULL,
    asset_id             BIGINT,
    understanding_json   JSON,
    evidence_hash        VARCHAR(64),
    coverage_report_json JSON,
    quality_status       VARCHAR(32) NOT NULL DEFAULT 'PENDING'
                           COMMENT 'PENDING|PASSED|NEED_CONFIRMATION|FAILED',
    quality_issues_json  JSON,
    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_tenant_project (tenant_id, project_id),
    INDEX idx_asset (asset_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE project_consistency_bibles (
    id                 BIGINT NOT NULL AUTO_INCREMENT,
    tenant_id          BIGINT NOT NULL,
    project_id         BIGINT NOT NULL,
    bible_json         JSON,
    source_version     VARCHAR(64),
    locked_fields_json JSON,
    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),
    UNIQUE KEY uk_tenant_project (tenant_id, project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE agent_run_plans (
    id                   BIGINT NOT NULL AUTO_INCREMENT,
    run_id               BIGINT NOT NULL,
    run_mode             VARCHAR(64) NOT NULL,
    reasoning_summary    TEXT,
    planned_steps_json   JSON,
    skipped_steps_json   JSON,
    step_inputs_json     JSON,
    quality_rules_json   JSON,
    model_policy_json    JSON,
    context_options_json JSON,
    idempotency_key      VARCHAR(128),
    plan_version         VARCHAR(32) NOT NULL DEFAULT 'v1',
    created_at           DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    UNIQUE KEY uk_run (run_id),
    INDEX idx_idempotency (idempotency_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE agent_prompt_snapshots (
    id                     BIGINT NOT NULL AUTO_INCREMENT,
    run_id                 BIGINT NOT NULL,
    step_key               VARCHAR(64) NOT NULL,
    prompt_role            VARCHAR(64) NOT NULL,
    system_prompt_version  VARCHAR(64),
    context_hash           VARCHAR(64),
    context_summary        TEXT,
    request_json           JSON,
    response_raw           LONGTEXT,
    parsed_output_json     JSON,
    validation_result_json JSON,
    model_provider         VARCHAR(64),
    model_id               VARCHAR(128),
    latency_ms             BIGINT,
    error_message          TEXT,
    created_at             DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    INDEX idx_run_step (run_id, step_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
