*** Settings ***
Documentation     Integration tests for v3 plan lifecycle: domain models, service layer,
...               automation levels, subplan support, and phase transitions.
...               Covers commit 548a09f stages A5, A6, E1, and their cross-module integration.
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${HELPER_SCRIPT}    robot/helper_plan_lifecycle_v3.py

*** Test Cases ***
Plan Full Lifecycle Through All Phases
    [Documentation]    Verify Action->Strategize->Execute->Apply (terminal) end-to-end via PlanLifecycleService
    [Tags]    lifecycle    plan    action    critical
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    lifecycle-full-cycle    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    lifecycle-full-cycle-ok

Action CRUD Operations Via Service
    [Documentation]    Verify action create, list, get, archive operations
    [Tags]    lifecycle    action    crud
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    action-crud    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    action-crud-ok

Plan Cancellation At Strategize Phase
    [Documentation]    Verify plans can be cancelled with reason during strategize
    [Tags]    lifecycle    plan    cancel
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    plan-cancellation    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plan-cancellation-ok

Plan Failure And Error State Handling
    [Documentation]    Verify plan failure sets error state and message correctly
    [Tags]    lifecycle    plan    error
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    plan-failure-recovery    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plan-failure-recovery-ok

Invalid Phase Transitions Raise Correct Errors
    [Documentation]    Verify that skipping phases or invalid transitions raise PlanNotReadyError
    [Tags]    lifecycle    plan    transitions    error
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    invalid-transitions    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    invalid-transitions-ok

Phase Transition Validation Model
    [Documentation]    Verify can_transition function validates all phase transition paths
    [Tags]    lifecycle    plan    model
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    phase-transitions    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    phase-transitions-ok

NamespacedName Parsing And String Round Trip
    [Documentation]    Verify NamespacedName.parse handles local, server, and standalone formats
    [Tags]    lifecycle    model    parsing
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    namespaced-name    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    namespaced-name-ok

Action Argument Parsing And Validation
    [Documentation]    Verify ActionArgument parse from string and validate_arguments on Action
    [Tags]    lifecycle    action    validation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    action-argument    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    action-argument-ok

Full Automation Auto Progresses Through Phases
    [Documentation]    Verify FULL_AUTOMATION level auto-advances from strategize to execute to apply
    [Tags]    lifecycle    automation    full
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    automation-full-auto    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    automation-full-auto-ok

Review Before Apply Pauses At Apply Boundary
    [Documentation]    Verify REVIEW_BEFORE_APPLY auto-executes but pauses before apply
    [Tags]    lifecycle    automation    review
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    automation-review-before-apply    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    automation-review-before-apply-ok

Automation Pause And Resume
    [Documentation]    Verify pause sets MANUAL and resume restores level
    [Tags]    lifecycle    automation    pause    resume
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    automation-pause-resume    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    automation-pause-resume-ok

Subplan Config And Failure Handler
    [Documentation]    Verify SubplanConfig, SubplanStatus, and SubplanFailureHandler integration
    [Tags]    lifecycle    subplan    model
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    subplan-model    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    subplan-model-ok

Plan With Subplan Configuration
    [Documentation]    Verify Plan model accepts subplan config and statuses via service
    [Tags]    lifecycle    subplan    plan
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    plan-with-subplans    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plan-with-subplans-ok

Plan Status Renders Action Name And Automation Profile Provenance
    [Documentation]    Verify plan status (as_cli_dict) includes action_name and automation profile provenance
    [Tags]    lifecycle    plan    status    cli
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    plan-status-rendering    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    plan-status-rendering-ok

Action YAML Config Loading And CLI Dict
    [Documentation]    Verify Action.from_config() loads YAML fields and as_cli_dict() renders them
    [Tags]    lifecycle    action    yaml
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    action-yaml-config    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    action-yaml-config-ok
