*** Settings ***
Documentation     Integration test: Workflow Example 5 — Database Schema Migration
...               with Safety Nets.  Exercises the ``review`` automation profile with
...               custom resource types, custom skills with database tools, phased
...               child plans, checkpointing, and rollback.
...               Spec reference: docs/specification.md ~lines 38244-38693
...               Issue: #769
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${HELPER}    robot/helper_int_wf05_db_migration.py

*** Test Cases ***
Register Custom Resource Type With Transaction Rollback Sandbox
    [Documentation]    Register local/postgres-db with transaction_rollback sandbox
    ...                and verify DB roundtrip.
    [Tags]    wf05    resource    registration
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    register-custom-resource-type    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Registration failed: ${result.stderr}
    Should Contain    ${result.stdout}    register-custom-resource-type-ok

Review Automation Profile Behavior
    [Documentation]    Verify the review profile has correct autonomy thresholds:
    ...                phases auto-start, decisions require approval, apply manual.
    [Tags]    wf05    profile    review
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    review-profile-behavior    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Profile check failed: ${result.stderr}
    Should Contain    ${result.stdout}    review-profile-behavior-ok

Create Custom Skill With Database Tools
    [Documentation]    Create local/database-ops skill with query_db,
    ...                execute_migration, and backfill_column tools.
    [Tags]    wf05    skill    database
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    create-custom-skill    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Skill creation failed: ${result.stderr}
    Should Contain    ${result.stdout}    create-custom-skill-ok

Action With Review Profile Creates Plan With Args
    [Documentation]    Create action with review profile, 4 typed arguments,
    ...                4 invariants, and verify plan creation with correct state.
    [Tags]    wf05    action    plan    review
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    action-with-review-profile    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Action/plan failed: ${result.stderr}
    Should Contain    ${result.stdout}    action-with-review-profile-ok

Checkpoint Creation And Rollback
    [Documentation]    Create checkpoints during simulated migration phases,
    ...                rollback to pre-migration state, verify file restoration.
    [Tags]    wf05    checkpoint    rollback    safety
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    checkpoint-and-rollback    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Checkpoint/rollback failed: ${result.stderr}
    Should Contain    ${result.stdout}    checkpoint-and-rollback-ok

Phased Subplan Execution For Sequential Migration
    [Documentation]    Spawn 5 child plans via SubplanService for sequential
    ...                migration phases with fail-fast and no retry.
    [Tags]    wf05    subplan    phased    sequential    spawn
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    phased-subplan-execution    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Subplan spawn failed: ${result.stderr}
    Should Contain    ${result.stdout}    phased-subplan-execution-ok

Plan Lifecycle With Review Profile Through Strategize
    [Documentation]    Create plan with review profile, run strategize via
    ...                PlanExecutor, verify transition to EXECUTE/QUEUED.
    [Tags]    wf05    lifecycle    strategize    review
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-lifecycle-review-profile    cwd=${WORKSPACE}    timeout=60s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0    Lifecycle test failed: ${result.stderr}
    Should Contain    ${result.stdout}    plan-lifecycle-review-profile-ok
