---
# Below is a huge hack for dynamically building a list of workers and finally assigning it to `matrix_synapse_workers_enabled_list`.
#
# set_fact within a loop does not work reliably in Ansible (it only executes on the first iteration for some reason),
# so we're forced to do something much uglier.

- name: Build generic workers
  ansible.builtin.set_fact:
    worker:
      type: 'generic_worker'
      instanceId: "{{ matrix_synapse_workers_generic_workers_port_range_start + item }}"
      port: "{{ matrix_synapse_workers_generic_workers_port_range_start + item }}"
      metrics_port: "{{ matrix_synapse_workers_generic_workers_metrics_range_start + item }}"
  register: "matrix_synapse_workers_list_results_generic_workers"
  loop: "{{ range(0, matrix_synapse_workers_generic_workers_count | int) | list }}"

- name: Build federation sender workers
  ansible.builtin.set_fact:
    worker:
      type: 'federation_sender'
      instanceId: "{{ item }}"
      port: 0
      metrics_port: "{{ matrix_synapse_workers_federation_sender_workers_metrics_range_start + item }}"
  register: "matrix_synapse_workers_list_results_federation_sender_workers"
  loop: "{{ range(0, matrix_synapse_workers_federation_sender_workers_count | int) | list }}"

# This type of worker can only have a count of 1, at most
- name: Build pusher workers
  ansible.builtin.set_fact:
    worker:
      type: 'pusher'
      instanceId: "{{ item }}"
      port: 0
      metrics_port: "{{ matrix_synapse_workers_pusher_workers_metrics_range_start + item }}"
  register: "matrix_synapse_workers_list_results_pusher_workers"
  loop: "{{ range(0, matrix_synapse_workers_pusher_workers_count | int) | list }}"

# This type of worker can only have a count of 1, at most
- name: Build appservice workers
  ansible.builtin.set_fact:
    worker:
      type: 'appservice'
      instanceId: "{{ item }}"
      port: 0
      metrics_port: "{{ matrix_synapse_workers_appservice_workers_metrics_range_start + item }}"
  register: "matrix_synapse_workers_list_results_appservice_workers"
  loop: "{{ range(0, matrix_synapse_workers_appservice_workers_count | int) | list }}"

- name: Build media_repository workers
  ansible.builtin.set_fact:
    worker:
      type: 'media_repository'
      instanceId: "{{ matrix_synapse_workers_media_repository_workers_port_range_start + item }}"
      port: "{{ matrix_synapse_workers_media_repository_workers_port_range_start + item }}"
      metrics_port: "{{ matrix_synapse_workers_media_repository_workers_metrics_range_start + item }}"
  register: "matrix_synapse_workers_list_results_media_repository_workers"
  loop: "{{ range(0, matrix_synapse_workers_media_repository_workers_count | int) | list }}"

- name: Build frontend_proxy workers
  ansible.builtin.set_fact:
    worker:
      type: 'frontend_proxy'
      instanceId: "{{ matrix_synapse_workers_frontend_proxy_workers_port_range_start + item }}"
      port: "{{ matrix_synapse_workers_frontend_proxy_workers_port_range_start + item }}"
      metrics_port: "{{ matrix_synapse_workers_frontend_proxy_workers_metrics_range_start + item }}"
  register: "matrix_synapse_workers_list_results_frontend_proxy_workers"
  loop: "{{ range(0, matrix_synapse_workers_frontend_proxy_workers_count | int) | list }}"

- ansible.builtin.set_fact:
    matrix_synapse_dynamic_workers_list: "{{ matrix_synapse_dynamic_workers_list | default([]) + [item.ansible_facts.worker] }}"
  with_items: |
    {{
      matrix_synapse_workers_list_results_generic_workers.results
      +
      matrix_synapse_workers_list_results_federation_sender_workers.results
      +
      matrix_synapse_workers_list_results_pusher_workers.results
      +
      matrix_synapse_workers_list_results_appservice_workers.results
      +
      matrix_synapse_workers_list_results_media_repository_workers.results
      +
      matrix_synapse_workers_list_results_frontend_proxy_workers.results
    }}

- ansible.builtin.set_fact:
    matrix_synapse_workers_enabled_list: "{{ matrix_synapse_dynamic_workers_list }}"
