# SPDX-FileCopyrightText: 2023 Slavi Pantaleev
# SPDX-FileCopyrightText: 2025 Suguru Hirahara
#
# SPDX-License-Identifier: AGPL-3.0-or-later

---
- name: Check existence of Matrix.to systemd service
  ansible.builtin.stat:
    path: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_matrixto_identifier }}.service"
  register: matrix_matrixto_service_stat

- name: Uninstall Matrix.to systemd services and files
  when: matrix_matrixto_service_stat.stat.exists | bool
  block:
    - name: Ensure Matrix.to systemd service is stopped
      ansible.builtin.service:
        name: "{{ matrix_matrixto_identifier }}"
        state: stopped
        enabled: false
        daemon_reload: true

    - name: Ensure Matrix.to systemd service does not exist
      ansible.builtin.file:
        path: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_matrixto_identifier }}.service"
        state: absent

    - name: Ensure Matrix.to container network does not exist via community.docker.docker_network
      when: devture_systemd_docker_base_container_network_creation_method == 'ansible-module'
      community.docker.docker_network:
        name: "{{ matrix_matrixto_container_network }}"
        state: absent

    - name: Ensure Matrix.to container network does not exist via ansible.builtin.command
      when: devture_systemd_docker_base_container_network_creation_method == 'command'
      ansible.builtin.command:
        cmd: >-
          {{ devture_systemd_docker_base_host_command_docker }} network rm
          {{ matrix_matrixto_container_network }}
      register: network_deletion_result
      changed_when: matrix_matrixto_container_network in network_deletion_result.stdout

    - name: Ensure Matrix.to path does not exist
      ansible.builtin.file:
        path: "{{ matrix_matrixto_base_path }}"
        state: absent
