---

- name: Check existence of matrix-coturn service
  ansible.builtin.stat:
    path: "{{ devture_systemd_docker_base_systemd_path }}/matrix-coturn.service"
  register: matrix_coturn_service_stat
  when: "not matrix_coturn_enabled | bool"

- when: matrix_coturn_service_stat.stat.exists | bool
  block:
    - name: Ensure matrix-coturn is stopped
      ansible.builtin.service:
        name: matrix-coturn
        state: stopped
        enabled: false
        daemon_reload: true

    - name: Ensure matrix-coturn-reload.timer is stopped
      ansible.builtin.service:
        name: matrix-coturn
        state: stopped
        enabled: false
        daemon_reload: true
      failed_when: false

    - name: Ensure systemd units don't exist
      ansible.builtin.file:
        path: "{{ devture_systemd_docker_base_systemd_path }}/{{ item }}"
        state: absent
      with_items:
        - matrix-coturn.service
        - matrix-coturn-reload.service
        - matrix-coturn-reload.timer

    - name: Ensure Matrix coturn paths don't exist
      ansible.builtin.file:
        path: "{{ matrix_coturn_base_path }}"
        state: absent