*** Settings ***
Documentation    Integration tests for v3_plans schema alignment (issue #921).
...              Verifies effective_profile_snapshot, root_plan_id self-reference,
...              and automation_profile NOT NULL default.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER}    ${CURDIR}/helper_plans_table_schema_alignment.py

*** Test Cases ***
Plan Includes Effective Profile Snapshot
    [Documentation]    Create a plan with an explicit profile snapshot and verify persistence.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    snapshot-roundtrip    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-snapshot-ok

Root Plan Self References Its Own Plan ID
    [Documentation]    Root plan must have root_plan_id == plan_id.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    root-self-ref    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-root-self-ref-ok

Child Plan Has Parent Root Plan ID
    [Documentation]    Child plan must carry the root ancestor's plan_id.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    child-root-id    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-child-root-ok

Grandchild Plan Carries Root Ancestor Plan ID
    [Documentation]    Grandchild plan (3-level hierarchy) must carry root ancestor's plan_id.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    grandchild-root-id    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-grandchild-root-ok

Automation Profile Defaults To Balanced And Roundtrips As None
    [Documentation]    Plan with no automation profile stores 'balanced' in DB and loads as None.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    automation-profile-default    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-automation-profile-default-ok

Automation Profile Ref Roundtrips Through Persistence
    [Documentation]    Plan with an explicit AutomationProfileRef persists and loads correctly.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    automation-profile-ref-roundtrip    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-automation-profile-ref-roundtrip-ok

NULL Root Plan ID Is Rejected By Database
    [Documentation]    Inserting a plan with NULL root_plan_id must raise IntegrityError.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    null-root-rejected    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-null-root-rejected-ok

Corrupted Effective Profile Snapshot Falls Back To Empty JSON
    [Documentation]    Corrupted effective_profile_snapshot in DB must fall back to '{}' on read.
    [Tags]    feature921
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    corrupted-snapshot-fallback    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    schema-corrupted-snapshot-fallback-ok
