---
- block:
    - pause:
        prompt: |
          Enter the API key (https://trailofbits.github.io/algo/cloud-cloudstack.html):
        echo: false
      register: _cs_key
      when:
        - cs_key is undefined
        - lookup('env', 'CLOUDSTACK_KEY')|length <= 0
      no_log: true

    - pause:
        prompt: |
          Enter the API secret (https://trailofbits.github.io/algo/cloud-cloudstack.html):
        echo: false
      register: _cs_secret
      when:
        - cs_secret is undefined
        - lookup('env', 'CLOUDSTACK_SECRET')|length <= 0
      no_log: true

    - pause:
        prompt: |
          Enter the API endpoint (https://trailofbits.github.io/algo/cloud-cloudstack.html)
      register: _cs_url
      when:
        - cs_url is undefined
        - lookup('env', 'CLOUDSTACK_ENDPOINT') | length <= 0

    - set_fact:
        algo_cs_key: "{{ cs_key | default(_cs_key.user_input | default(None)) | default(lookup('env', 'CLOUDSTACK_KEY'), true) }}"
        algo_cs_token: "{{ cs_secret | default(_cs_secret.user_input | default(None)) | default(lookup('env', 'CLOUDSTACK_SECRET'), true) }}"
        algo_cs_url: >-
          {{ cs_url | default(_cs_url.user_input|default(None)) |
             default(lookup('env', 'CLOUDSTACK_ENDPOINT'), true) }}
      no_log: true

    - name: Check for Exoscale API endpoint
      fail:
        msg: |
          ERROR: Exoscale CloudStack API has been deprecated as of May 1, 2024.

          Exoscale has migrated from CloudStack to their proprietary API v2, which is not compatible
          with CloudStack-based tools. Algo no longer supports Exoscale deployments.

          Please consider these alternative providers:
          - Hetzner (provider: hetzner) - German provider with European coverage
          - DigitalOcean (provider: digitalocean) - Has Amsterdam and Frankfurt regions
          - Vultr (provider: vultr) - Multiple European locations
          - Scaleway (provider: scaleway) - French provider

          If you're using a different CloudStack provider, please provide the correct API endpoint.
      when: "'exoscale.com' in algo_cs_url or 'exoscale.ch' in algo_cs_url"

    - name: Get zones on cloud
      cs_zone_info:
      register: _cs_zones
      environment:
        CLOUDSTACK_KEY: "{{ algo_cs_key }}"
        CLOUDSTACK_SECRET: "{{ algo_cs_token }}"
        CLOUDSTACK_ENDPOINT: "{{ algo_cs_url }}"
      no_log: true

    - name: Extract zones from output
      set_fact:
        cs_zones: "{{ _cs_zones['zones'] | sort(attribute='name') }}"

    - name: Set the default zone
      set_fact:
        default_zone: "1"  # Default to first zone in the list

    - pause:
        prompt: |
          What zone should the server be located in?
            {% for z in cs_zones %}
            {{ loop.index }}. {{ z['name'] }}
            {% endfor %}

            Enter the number of your desired zone
            [{{ default_zone }}]
      register: _algo_region
      when: region is undefined
