*** Settings ***
Documentation     Integration tests for context delete with --all and --yes flags
...               SEC1: Uses simple_echo_config.yaml which requires code blocks.
Library           Process
Library           OperatingSystem
Library           String
Library           DateTime
Resource    ${CURDIR}/v2_paths.resource
Force Tags        code_blocks
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${PYTHON}              python
${SIMPLE_CONFIG}            ${V2_CONFIG_DIR}/simple_echo_config.yaml
${CONTEXT_DIR}              ${TEMPDIR}/test_contexts_delete_all_yes
${UNIQUE_ID}                ${EMPTY}
${TEMP}                     ${EMPTY}

*** Test Cases ***
Test Delete Single Context With Yes Flag
    [Documentation]    Test context delete with --yes bypasses confirmation
    ${context_name} =    Set Variable    delete_yes_${UNIQUE_ID}

    # Create context
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test message"

    Directory Should Exist    ${CONTEXT_DIR}/${context_name}

    # Delete it with --yes flag
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --yes

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Not Exist    ${CONTEXT_DIR}/${context_name}
    Should Contain    ${result.stdout}    deleted

Test Delete Single Context With Short Yes Flag
    [Documentation]    Test context delete with -y shorthand flag
    ${context_name} =    Set Variable    delete_short_${UNIQUE_ID}

    # Create context
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test message"

    Directory Should Exist    ${CONTEXT_DIR}/${context_name}

    # Delete it with -y flag
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    -y

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Not Exist    ${CONTEXT_DIR}/${context_name}

Test Delete Single Context With Confirmation Accept
    [Documentation]    Test context delete with confirmation accepted
    ${context_name} =    Set Variable    delete_confirm_${UNIQUE_ID}

    # Create context
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test message"

    Directory Should Exist    ${CONTEXT_DIR}/${context_name}

    # Delete with confirmation (answer 'y')
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    stdin=y\n

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Not Exist    ${CONTEXT_DIR}/${context_name}

Test Delete Single Context With Confirmation Reject
    [Documentation]    Test context delete with confirmation rejected
    ${context_name} =    Set Variable    delete_reject_${UNIQUE_ID}

    # Create context
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test message"

    Directory Should Exist    ${CONTEXT_DIR}/${context_name}

    # Delete with confirmation rejected (answer 'n')
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    ${context_name}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    stdin=n\n

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Exist    ${CONTEXT_DIR}/${context_name}
    Should Contain    ${result.stdout}    cancelled

Test Delete All Contexts With All And Yes Flags
    [Documentation]    Test delete all contexts with --all --yes
    [Setup]    Empty Directory    ${CONTEXT_DIR}
    ${ctx1} =    Set Variable    all_ctx1_${UNIQUE_ID}
    ${ctx2} =    Set Variable    all_ctx2_${UNIQUE_ID}
    ${ctx3} =    Set Variable    all_ctx3_${UNIQUE_ID}

    # Create multiple contexts
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx1}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx2}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx3}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Directory Should Exist    ${CONTEXT_DIR}/${ctx1}
    Directory Should Exist    ${CONTEXT_DIR}/${ctx2}
    Directory Should Exist    ${CONTEXT_DIR}/${ctx3}

    # Delete all with --all --yes
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    --all
    ...    --yes
    ...    --context-dir    ${CONTEXT_DIR}

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Not Exist    ${CONTEXT_DIR}/${ctx1}
    Directory Should Not Exist    ${CONTEXT_DIR}/${ctx2}
    Directory Should Not Exist    ${CONTEXT_DIR}/${ctx3}
    Should Contain    ${result.stdout}    Deleted 3 context

Test Delete All With Confirmation Accept
    [Documentation]    Test delete all with confirmation accepted
    [Setup]    Empty Directory    ${CONTEXT_DIR}
    ${ctx1} =    Set Variable    all_confirm1_${UNIQUE_ID}
    ${ctx2} =    Set Variable    all_confirm2_${UNIQUE_ID}

    # Create contexts
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx1}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx2}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    # Delete all with confirmation
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    --all
    ...    --context-dir    ${CONTEXT_DIR}
    ...    stdin=y\n

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Not Exist    ${CONTEXT_DIR}/${ctx1}
    Directory Should Not Exist    ${CONTEXT_DIR}/${ctx2}
    Should Contain    ${result.stdout}    Found
    Should Contain    ${result.stdout}    to delete

Test Delete All With Confirmation Reject
    [Documentation]    Test delete all with confirmation rejected
    [Setup]    Empty Directory    ${CONTEXT_DIR}
    ${ctx1} =    Set Variable    all_reject1_${UNIQUE_ID}
    ${ctx2} =    Set Variable    all_reject2_${UNIQUE_ID}

    # Create contexts
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx1}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx2}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    # Delete all with confirmation rejected
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    --all
    ...    --context-dir    ${CONTEXT_DIR}
    ...    stdin=n\n

    Should Be Equal As Integers    ${result.rc}    0
    Directory Should Exist    ${CONTEXT_DIR}/${ctx1}
    Directory Should Exist    ${CONTEXT_DIR}/${ctx2}
    Should Contain    ${result.stdout}    cancelled

Test Error When Both Name And All Provided
    [Documentation]    Test error when both NAME and --all are provided
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    test_context
    ...    --all
    ...    --context-dir    ${CONTEXT_DIR}

    Should Not Be Equal As Integers    ${result.rc}    0
    Should Contain Any    ${result.stderr}    ${result.stdout}    Cannot specify NAME when using --all

Test Error When Neither Name Nor All Provided
    [Documentation]    Test error when neither NAME nor --all is provided
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    --context-dir    ${CONTEXT_DIR}

    Should Not Be Equal As Integers    ${result.rc}    0
    Should Contain Any    ${result.stderr}    ${result.stdout}    Must specify NAME or use --all

Test Delete All When No Contexts Exist
    [Documentation]    Test delete all when directory is empty
    # Ensure directory is empty
    Empty Directory    ${CONTEXT_DIR}

    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    --all
    ...    --yes
    ...    --context-dir    ${CONTEXT_DIR}

    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    No contexts found

Test Delete Non Existent Context
    [Documentation]    Test deleting a context that doesn't exist
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    non_existent_context_${UNIQUE_ID}
    ...    --yes
    ...    --context-dir    ${CONTEXT_DIR}

    Should Not Be Equal As Integers    ${result.rc}    0
    Should Contain Any    ${result.stderr}    ${result.stdout}    does not exist

Test Delete All Shows Context List Before Deletion
    [Documentation]    Test that --all shows context list before confirmation
    [Setup]    Empty Directory    ${CONTEXT_DIR}
    ${ctx1} =    Set Variable    list_ctx1_${UNIQUE_ID}
    ${ctx2} =    Set Variable    list_ctx2_${UNIQUE_ID}
    ${ctx3} =    Set Variable    list_ctx3_${UNIQUE_ID}

    # Create contexts
    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx1}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx2}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    Run Process    ${PYTHON}    -m    cleveragents    actor    run
    ...    -c    ${SIMPLE_CONFIG}
    ...    --context    ${ctx3}
    ...    --context-dir    ${CONTEXT_DIR}
    ...    --unsafe
    ...    --allow-rxpy-in-run-mode
    ...    test-actor    "test"

    # Delete all with confirmation to see list
    ${result} =    Run Process    ${PYTHON}    -m    cleveragents    context    delete
    ...    --all
    ...    --context-dir    ${CONTEXT_DIR}
    ...    stdin=n\n

    Should Contain    ${result.stdout}    Found 3 context
    Should Contain    ${result.stdout}    ${ctx1}
    Should Contain    ${result.stdout}    ${ctx2}
    Should Contain    ${result.stdout}    ${ctx3}

*** Keywords ***
Setup Test Environment
    ${timestamp} =    Get Current Date    result_format=%Y%m%d_%H%M%S
    ${random} =    Evaluate    random.randint(1000, 9999)    modules=random
    Set Suite Variable    ${UNIQUE_ID}    ${timestamp}_${random}
    Set Suite Variable    ${TEMP}    ${TEMPDIR}/ca_delete_test_${UNIQUE_ID}
    Set Suite Variable    ${CONTEXT_DIR}    ${TEMP}/contexts
    Create Directory    ${TEMP}
    Create Directory    ${CONTEXT_DIR}

Cleanup Test Environment
    Run Keyword And Ignore Error    Remove Directory    ${TEMP}    recursive=True

Should Contain Any
    [Arguments]    ${text1}    ${text2}    ${expected}
    ${combined} =    Set Variable    ${text1}${text2}
    Should Contain    ${combined}    ${expected}
