---
# Linode/cleanup.yml
- name: Clean up Linode infrastructure
  hosts: localhost
  connection: local
  gather_facts: false
  vars_files:
    - vars.yaml
  vars:
    cleanup_redirector: "{{ (redirector_name is defined and redirector_name != '') | ternary(true, false) }}"
    cleanup_c2: "{{ (c2_name is defined and c2_name != '') | ternary(true, false) }}"
    cleanup_tracker: "{{ (tracker_name is defined and tracker_name != '') | ternary(true, false) }}"
    cleanup_attack_box: "{{ (attack_box_name is defined and attack_box_name != '') | ternary(true, false) }}"
    confirm_cleanup: false  # Skip confirmation in automated teardown

  tasks:
    - name: Validate required Linode token
      assert:
        that:
          - linode_token is defined and linode_token != ""
        fail_msg: "Linode API token is required. Set linode_token in vars.yaml or via --linode-token."

    - name: Cleanup resources notice
      debug:
        msg: |
          ===============================================
          CLEANUP OPERATION
          ===============================================
          The following resources will be DELETED PERMANENTLY:
          {% if cleanup_redirector and redirector_name is defined %}
          - Redirector instance: {{ redirector_name }}
          {% endif %}
          {% if cleanup_c2 and c2_name is defined %}
          - C2 instance: {{ c2_name }}
          {% endif %}
          {% if cleanup_tracker and tracker_name is defined %}
          - Tracker instance: {{ tracker_name }}
          {% endif %}
          {% if cleanup_attack_box and attack_box_name is defined %}
          - Attack Box instance: {{ attack_box_name }}
          {% endif %}
      when: confirm_cleanup | bool

    - name: Confirm cleanup operation
      pause:
        prompt: "\n>>> Type 'yes' to confirm deletion or press Ctrl+C to abort <<<"
      register: confirmation
      when: confirm_cleanup | bool

    - name: Skip cleanup if not confirmed
      meta: end_play
      when: confirm_cleanup | bool and (confirmation.user_input | default('')) != 'yes'
      
    - name: Delete redirector instance
      community.general.linode_v4:
        access_token: "{{ linode_token }}"
        label: "{{ redirector_name }}"
        state: absent
      when: cleanup_redirector and redirector_name is defined and redirector_name != ""
      register: redirector_deletion
      ignore_errors: yes
      
    - name: Delete C2 instance
      community.general.linode_v4:
        access_token: "{{ linode_token }}"
        label: "{{ c2_name }}"
        state: absent
      when: cleanup_c2 and c2_name is defined and c2_name != ""
      register: c2_deletion
      ignore_errors: yes
      
    - name: Delete tracker instance
      community.general.linode_v4:
        access_token: "{{ linode_token }}"
        label: "{{ tracker_name }}"
        state: absent
      when: cleanup_tracker and tracker_name is defined and tracker_name != ""
      register: tracker_deletion
      ignore_errors: yes

    - name: Delete attack box instance
      community.general.linode_v4:
        access_token: "{{ linode_token }}"
        label: "{{ attack_box_name }}"
        state: absent
      when: cleanup_attack_box and attack_box_name is defined and attack_box_name != ""
      register: attack_box_deletion
      ignore_errors: yes

    - name: Clean up deployment state file
      file:
        path: "{{ playbook_dir }}/../deployment_state_{{ deployment_id }}.json"
        state: absent
      ignore_errors: yes
      register: state_file_deletion

    - name: Report state file cleanup
      debug:
        msg: "Deployment state file {{ 'deleted' if state_file_deletion.changed else 'not found' }}"
      when: state_file_deletion is defined
      
    - name: Report cleanup status
      debug:
        msg: |
          Cleanup results:
          {% if redirector_deletion is defined and redirector_name is defined %}
          - Redirector {{ redirector_name }}: {{ redirector_deletion.changed | ternary('Deleted', 'Not found/Could not delete') }}
          {% endif %}
          {% if c2_deletion is defined and c2_name is defined %}
          - C2 {{ c2_name }}: {{ c2_deletion.changed | ternary('Deleted', 'Not found/Could not delete') }}
          {% endif %}
          {% if tracker_deletion is defined and tracker_name is defined %}
          - Tracker {{ tracker_name }}: {{ tracker_deletion.changed | ternary('Deleted', 'Not found/Could not delete') }}
          {% endif %}
          {% if attack_box_deletion is defined and attack_box_name is defined %}
          - Attack Box {{ attack_box_name }}: {{ attack_box_deletion.changed | ternary('Deleted', 'Not found/Could not delete') }}
          {% endif %}