---
- name: Validate required variables
  assert:
    that:
      - komodo_stack_name is defined and komodo_stack_name | length > 0
      - komodo_server_name is defined and komodo_server_name | length > 0
      - komodo_repo_name is defined and komodo_repo_name | length > 0
      - komodo_run_directory is defined and komodo_run_directory | length > 0
      - komodo_api_key is defined and komodo_api_key | length > 0
      - komodo_api_secret is defined and komodo_api_secret | length > 0
    fail_msg: "Missing required variables: komodo_stack_name, komodo_server_name, komodo_repo_name, komodo_run_directory, komodo_api_key, or komodo_api_secret"

- name: Get Komodo server by name
  uri:
    url: "{{ komodo_core_address }}/read/GetServer"
    method: POST
    headers:
      "X-Api-Key": "{{ komodo_api_key }}"
      "X-Api-Secret": "{{ komodo_api_secret }}"
      "Content-Type": "application/json"
    body_format: json
    body:
      server: "{{ komodo_server_name }}"
    status_code: 200
    validate_certs: false
  register: komodo_server
  retries: 3
  delay: 2

- name: Extract server ID
  set_fact:
    komodo_server_id: "{{ komodo_server.json._id['$oid'] }}"

- name: Debug server response
  debug:
    msg: "Server ID: {{ komodo_server_id }}"

- name: Get Komodo repo by name
  uri:
    url: "{{ komodo_core_address }}/read/GetRepo"
    method: POST
    headers:
      "X-Api-Key": "{{ komodo_api_key }}"
      "X-Api-Secret": "{{ komodo_api_secret }}"
      "Content-Type": "application/json"
    body_format: json
    body:
      repo: "{{ komodo_repo_name }}"
    status_code: 200
    validate_certs: false
  register: komodo_repo
  retries: 3
  delay: 2

- name: Extract repo ID
  set_fact:
    komodo_repo_id: "{{ komodo_repo.json._id['$oid'] }}"

- name: Debug repo response
  debug:
    msg: "Repo ID: {{ komodo_repo_id }}"

- name: Check if stack already exists
  uri:
    url: "{{ komodo_core_address }}/read/GetStack"
    method: POST
    headers:
      "X-Api-Key": "{{ komodo_api_key }}"
      "X-Api-Secret": "{{ komodo_api_secret }}"
      "Content-Type": "application/json"
    body_format: json
    body:
      stack: "{{ komodo_stack_name }}"
    status_code: [200, 500]
    validate_certs: false
  register: existing_stack
  retries: 3
  delay: 2
  failed_when: false

- name: Create Komodo stack
  uri:
    url: "{{ komodo_core_address }}/write/CreateStack"
    method: POST
    headers:
      "X-Api-Key": "{{ komodo_api_key }}"
      "X-Api-Secret": "{{ komodo_api_secret }}"
      "Content-Type": "application/json"
    body_format: json
    body:
      name: "{{ komodo_stack_name }}"
      config:
        server_id: "{{ komodo_server_id }}"
        linked_repo: "{{ komodo_repo_id }}"
        run_directory: "{{ komodo_run_directory }}"
    status_code: [200, 201]
    validate_certs: false
  register: stack_result
  when: existing_stack.status == 500
  retries: 3
  delay: 2

- name: Display stack creation result
  debug:
    msg: "Stack '{{ komodo_stack_name }}' created successfully with ID: {{ stack_result.json._id }}"
  when: existing_stack.status == 500 and stack_result is succeeded

- name: Display stack already exists message
  debug:
    msg: "Stack '{{ komodo_stack_name }}' already exists with ID: {{ existing_stack.json._id }}"
  when: existing_stack.status == 200

- name: Deploy stack
  uri:
    url: "{{ komodo_core_address }}/execute/DeployStack"
    method: POST
    headers:
      "X-Api-Key": "{{ komodo_api_key }}"
      "X-Api-Secret": "{{ komodo_api_secret }}"
      "Content-Type": "application/json"
    body_format: json
    body:
      stack: "{{ komodo_stack_name }}"
    status_code: 200
    validate_certs: false
  register: start_result
  retries: 3
  delay: 2
  when: existing_stack.status == 500 and stack_result is succeeded
