# SPDX-FileCopyrightText: 2019 - 2023 Slavi Pantaleev
# SPDX-FileCopyrightText: 2019 Dan Arnfield
# SPDX-FileCopyrightText: 2019 Edgars Voroboks
# SPDX-FileCopyrightText: 2020 Chris van Dijk
# SPDX-FileCopyrightText: 2022 Marko Weltzer
# SPDX-FileCopyrightText: 2025 Suguru Hirahara
#
# SPDX-License-Identifier: AGPL-3.0-or-later

---

- name: Fail if required Dimension settings not defined
  ansible.builtin.fail:
    msg: >
      You need to define a required configuration setting (`{{ item.name }}`).
  when: "item.when | bool and lookup('vars', item.name, default='') | string | length == 0"
  with_items:
    - {'name': 'matrix_dimension_hostname', when: true}
    - {'name': 'matrix_dimension_container_network', when: true}
    - {'name': 'matrix_dimension_access_token', when: true}
    - {'name': 'matrix_dimension_homeserver_clientServerUrl', when: true}
    - {'name': 'matrix_dimension_homeserver_federationUrl', when: true}
    - {'name': 'matrix_dimension_homeserver_mediaUrl', when: true}
    - {'name': 'matrix_dimension_database_hostname', when: "{{ matrix_dimension_database_engine == 'postgres' }}"}

- name: (Deprecation) Catch and report renamed Dimension variables
  ansible.builtin.fail:
    msg: >-
      Your configuration contains a variable, which now has a different name.
      Please rename the variable (`{{ item.old }}` -> `{{ item.new }}`) on your configuration file (vars.yml).
  when: "lookup('ansible.builtin.varnames', ('^' + item.old + '$'), wantlist=True) | length > 0"
  with_items:
    - {'old': 'matrix_dimension_container_expose_port', 'new': '<superseded by matrix_dimension_container_http_host_bind_port>'}
    - {'old': 'matrix_dimension_container_image_name_prefix', 'new': 'matrix_dimension_docker_image_registry_prefix'}

- when: matrix_dimension_container_labels_traefik_enabled | bool
  block:
    - name: Fail if required Dimension Traefik settings not defined
      ansible.builtin.fail:
        msg: >-
          You need to define a required configuration setting (`{{ item }}`).
      when: "vars[item] == ''"
      with_items:
        - matrix_dimension_container_labels_traefik_hostname
        - matrix_dimension_container_labels_traefik_path_prefix

    # We ensure it doesn't end with a slash, because we handle both (slash and no-slash).
    # Knowing that `matrix_dimension_container_labels_traefik_path_prefix` does not end with a slash
    # ensures we know how to set these routes up without having to do "does it end with a slash" checks elsewhere.
    - name: Fail if matrix_dimension_container_labels_traefik_path_prefix ends with a slash
      ansible.builtin.fail:
        msg: >-
          matrix_dimension_container_labels_traefik_path_prefix (`{{ matrix_dimension_container_labels_traefik_path_prefix }}`) must either be `/` or not end with a slash (e.g. `/dimension`).
      when: "matrix_dimension_container_labels_traefik_path_prefix != '/' and matrix_dimension_container_labels_traefik_path_prefix[-1] == '/'"

    - name: Fail if matrix_dimension_container_labels_traefik_path_prefix is not /
      ansible.builtin.fail:
        msg: >-
          matrix_dimension_container_labels_traefik_path_prefix (`{{ matrix_dimension_container_labels_traefik_path_prefix }}`) must currently be set to `/`.
          Other values are not supported.
          See: https://github.com/turt2live/matrix-dimension/issues/510
      when: "matrix_dimension_container_labels_traefik_path_prefix != '/' and matrix_dimension_container_labels_traefik_path_prefix[-1] == '/'"
