# SPDX-FileCopyrightText: 2019 - 2020 MDAD project contributors
# SPDX-FileCopyrightText: 2019 - 2024 Slavi Pantaleev
# SPDX-FileCopyrightText: 2019 Dan Arnfield
# SPDX-FileCopyrightText: 2020 Chris van Dijk
# SPDX-FileCopyrightText: 2020 Stuart Mumford
# SPDX-FileCopyrightText: 2022 Jim Myhrberg
# SPDX-FileCopyrightText: 2022 Marko Weltzer
# SPDX-FileCopyrightText: 2022 Nikita Chernyi
# SPDX-FileCopyrightText: 2022 Sebastian Gumprich
# SPDX-FileCopyrightText: 2024 David Mehren
#
# SPDX-License-Identifier: AGPL-3.0-or-later

---

- ansible.builtin.set_fact:
    matrix_appservice_discord_requires_restart: false

- when: "matrix_appservice_discord_database_engine == 'postgres'"
  block:
    - name: Check if an SQLite database already exists
      ansible.builtin.stat:
        path: "{{ matrix_appservice_discord_sqlite_database_path_local }}"
      register: matrix_appservice_discord_sqlite_database_path_local_stat_result

    - when: "matrix_appservice_discord_sqlite_database_path_local_stat_result.stat.exists | bool"
      block:
        - ansible.builtin.include_role:
            name: galaxy/postgres
            tasks_from: migrate_db_to_postgres
          vars:
            postgres_db_migration_request:
              src: "{{ matrix_appservice_discord_sqlite_database_path_local }}"
              dst: "{{ matrix_appservice_discord_database_connString }}"
              caller: "{{ role_path | basename }}"
              engine_variable_name: 'matrix_appservice_discord_database_engine'
              engine_old: 'sqlite'
              systemd_services_to_stop: ['matrix-appservice-discord.service']

        - ansible.builtin.set_fact:
            matrix_appservice_discord_requires_restart: true

- name: Ensure Appservice Discord image is pulled
  community.docker.docker_image:
    name: "{{ matrix_appservice_discord_docker_image }}"
    source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
    force_source: "{{ matrix_appservice_discord_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
    force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_appservice_discord_docker_image_force_pull }}"
  register: result
  retries: "{{ devture_playbook_help_container_retries_count }}"
  delay: "{{ devture_playbook_help_container_retries_delay }}"
  until: result is not failed

- name: Ensure AppService Discord paths exist
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    mode: 0750
    owner: "{{ matrix_user_name }}"
    group: "{{ matrix_group_name }}"
  with_items:
    - "{{ matrix_appservice_discord_base_path }}"
    - "{{ matrix_appservice_discord_config_path }}"
    - "{{ matrix_appservice_discord_data_path }}"

- name: Check if an old database file already exists
  ansible.builtin.stat:
    path: "{{ matrix_appservice_discord_base_path }}/discord.db"
  register: matrix_appservice_discord_stat_db

- name: (Data relocation) Ensure matrix-appservice-discord.service is stopped
  ansible.builtin.service:
    name: matrix-appservice-discord
    state: stopped
    enabled: false
    daemon_reload: true
  failed_when: false
  when: "matrix_appservice_discord_stat_db.stat.exists"

- name: (Data relocation) Move AppService Discord discord.db file to ./data directory
  ansible.builtin.command:
    cmd: "mv {{ matrix_appservice_discord_base_path }}/{{ item }} {{ matrix_appservice_discord_data_path }}/{{ item }}"
    creates: "{{ matrix_appservice_discord_data_path }}/{{ item }}"
    removes: "{{ matrix_appservice_discord_base_path }}/{{ item }}"
  with_items:
    - discord.db
    - user-store.db
    - room-store.db
  when: "matrix_appservice_discord_stat_db.stat.exists"

- name: Ensure AppService Discord config.yaml installed
  ansible.builtin.copy:
    content: "{{ matrix_appservice_discord_configuration | to_nice_yaml(indent=2, width=999999) }}"
    dest: "{{ matrix_appservice_discord_config_path }}/config.yaml"
    mode: 0644
    owner: "{{ matrix_user_name }}"
    group: "{{ matrix_group_name }}"

- name: Ensure AppService Discord registration.yaml installed
  ansible.builtin.copy:
    content: "{{ matrix_appservice_discord_registration | to_nice_yaml(indent=2, width=999999) }}"
    dest: "{{ matrix_appservice_discord_config_path }}/registration.yaml"
    mode: 0644
    owner: "{{ matrix_user_name }}"
    group: "{{ matrix_group_name }}"

# If `matrix_appservice_discord_client_id` hasn't changed, the same invite link would be generated.
# We intentionally suppress Ansible changes.
- name: Generate AppService Discord invite link
  ansible.builtin.shell: >-
    {{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-appservice-discord-link-gen
    --user={{ matrix_user_uid }}:{{ matrix_user_gid }}
    --cap-drop=ALL
    --mount type=bind,src={{ matrix_appservice_discord_config_path }},dst=/cfg
    -w /cfg
    {{ matrix_appservice_discord_docker_image }}
    /bin/sh -c "node /build/tools/addbot.js > /cfg/invite_link"
  changed_when: false

- name: Ensure matrix-appservice-discord container network is created
  community.general.docker_network:
    enable_ipv6: "{{ devture_systemd_docker_base_ipv6_enabled }}"
    name: "{{ matrix_appservice_discord_container_network }}"
    driver: bridge
    driver_options: "{{ devture_systemd_docker_base_container_networks_driver_options }}"

- name: Ensure matrix-appservice-discord.service installed
  ansible.builtin.template:
    src: "{{ role_path }}/templates/systemd/matrix-appservice-discord.service.j2"
    dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-appservice-discord.service"
    mode: 0644
  register: matrix_appservice_discord_systemd_service_result

- name: Ensure matrix-appservice-discord.service restarted, if necessary
  ansible.builtin.service:
    name: "matrix-appservice-discord.service"
    state: restarted
    daemon_reload: true
  when: "matrix_appservice_discord_requires_restart | bool"
