- name: Ensure MAILU_HOSTNAMES is a list with max 1 entry
  ansible.builtin.assert:
    that:
      - MAILU_HOSTNAMES is iterable
      - MAILU_HOSTNAMES is sequence
      - MAILU_HOSTNAMES | length <= 1
    fail_msg: "MAILU_HOSTNAMES must be a list with at most one entry (only one host is supported). You can set the other ones as alias."
    success_msg: "MAILU_HOSTNAMES is valid."

- name: "Mailu Docker and Webserver Setup"
  block:
  - name: "load docker, db and proxy for {{ application_id }}"
    include_role: 
      name: cmp-db-docker-proxy
    vars:
      docker_compose_flush_handlers: true

  - name: "Include Cert deploy service for '{{ role_name }}'"
    include_role:
      name: sys-ctl-mtn-cert-deploy
  vars:
    domain: "{{ MAILU_HOSTNAME }}"

- name: Flush docker service handlers
  meta: flush_handlers

- name: "Create Mailu accounts"
  include_tasks: 02_create-user.yml
  vars:
    MAILU_DOCKER_DIR:        "{{ docker_compose.directories.instance }}"
    mailu_api_base_url:       "http://127.0.0.1:8080/api/v1"
    mailu_action: >-
      {{
        (
          'administrator' in (item.value.get('roles', []))
        )
        | ternary('admin','user')
      }}
    mailu_user_key:           "{{ item.key }}"
    mailu_user_name:          "{{ item.value.username }}"
    mailu_password:           "{{ item.value.password }}"
    mailu_token_ip:           "{{ item.value.ip | default('') }}"
  loop:                       "{{ users | dict2items }}"
  loop_control:
    loop_var: item
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: Generate DKIM public key
  include_tasks: 04_generate-and-read-dkim.yml

- name: Set Mailu DNS records
  include_tasks: 05_dns-records.yml
