# SPDX-FileCopyrightText: 2024 David Mehren
# SPDX-FileCopyrightText: 2024 - 2025 Slavi Pantaleev
#
# SPDX-License-Identifier: AGPL-3.0-or-later

---

- name: Ensure Element Admin paths exist
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: directory
    mode: '0750'
    owner: "{{ matrix_user_name }}"
    group: "{{ matrix_group_name }}"
  with_items:
    - path: "{{ matrix_element_admin_base_path }}"
      when: true
    - path: "{{ matrix_element_admin_container_src_path }}"
      when: "{{ matrix_element_admin_container_image_self_build }}"
  when: item.when | bool

- name: Ensure Element Admin support files installed
  ansible.builtin.template:
    src: "{{ role_path }}/templates/{{ item }}.j2"
    dest: "{{ matrix_element_admin_base_path }}/{{ item }}"
    mode: '0640'
    owner: "{{ matrix_user_name }}"
    group: "{{ matrix_group_name }}"
  with_items:
    - labels
    - env
  register: matrix_element_admin_support_files_result

- name: Ensure Element Admin container image is pulled
  community.docker.docker_image_pull:
    name: "{{ matrix_element_admin_container_image }}"
    pull: always
  when: "not matrix_element_admin_container_image_self_build | bool"
  register: matrix_element_admin_container_image_pull_result
  retries: "{{ devture_playbook_help_container_retries_count }}"
  delay: "{{ devture_playbook_help_container_retries_delay }}"
  until: matrix_element_admin_container_image_pull_result is not failed

- when: matrix_element_admin_container_image_self_build | bool
  block:
    - name: Ensure Element Admin repository is present on self-build
      ansible.builtin.git:
        repo: "{{ matrix_element_admin_container_image_self_build_repo }}"
        version: "{{ matrix_element_admin_container_image_self_build_repo_version }}"
        dest: "{{ matrix_element_admin_container_src_path }}"
        force: "yes"
      become: true
      become_user: "{{ matrix_user_name }}"
      register: matrix_element_admin_git_pull_results

    - name: Ensure Element Admin container image is built
      ansible.builtin.command:
        cmd: |-
          {{ devture_systemd_docker_base_host_command_docker }} buildx build
          --tag={{ matrix_element_admin_container_image }}
          --file={{ matrix_element_admin_container_src_path }}/Dockerfile
          {{ matrix_element_admin_container_src_path }}
      changed_when: true

- name: Ensure Element Admin container network is created
  when: matrix_element_admin_container_network != 'host'
  community.general.docker_network:
    enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
    name: "{{ matrix_element_admin_container_network }}"
    driver: bridge
    driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"

- name: Ensure Element Admin systemd service installed
  ansible.builtin.template:
    src: "{{ role_path }}/templates/systemd/matrix-element-admin.service.j2"
    dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-element-admin.service"
    mode: '0644'
  register: matrix_element_admin_systemd_service_result

- name: Determine whether Element Admin needs a restart
  ansible.builtin.set_fact:
    matrix_element_admin_restart_necessary: >-
      {{
        matrix_element_admin_support_files_result.changed | default(false)
        or matrix_element_admin_systemd_service_result.changed | default(false)
        or matrix_element_admin_container_image_pull_result.changed | default(false)
      }}
