---
- name: Deploy Shadowsocks-Rust Proxy Configurations
  hosts: all
  gather_facts: yes
  tasks:
    - name: Deploy Shadowsocks-Rust proxy services
      block:
        - name: Ensure base directory for shadowsocks exists
          ansible.builtin.file:
            path: "/srv/shadowsocks-rust"
            state: directory
            owner: "{{ ansible_user }}"
            group: "{{ deploy_group }}"
            mode: '0755'

        - name: Create individual proxy config directories
          ansible.builtin.file:
            path: "/srv/shadowsocks-rust/config_ssp_{{ item.value.local_port }}"
            state: directory
            owner: "{{ ansible_user }}"
            group: "{{ deploy_group }}"
            mode: '0755'
          loop: "{{ shadowsocks_proxies | dict2items }}"

        - name: Create Shadowsocks-Rust proxy configuration files
          ansible.builtin.copy:
            content: |
              {
                  "server": "{{ item.value.server }}",
                  "server_port": {{ item.value.server_port }},
                  "password": "{{ lookup('vars', item.value.vault_password_key) }}",
                  "local_address": "{{ shadowsocks_local_address }}",
                  "local_port": {{ item.value.local_port }},
                  "timeout": {{ shadowsocks_timeout }},
                  "method": "{{ shadowsocks_cipher_method }}",
                  "fast_open": {{ shadowsocks_fast_open | to_json }},
                  "mode": "{{ shadowsocks_mode }}"
              }
            dest: "/srv/shadowsocks-rust/config_ssp_{{ item.value.local_port }}/config.json"
            owner: "{{ ansible_user }}"
            group: "{{ deploy_group }}"
            mode: '0644'
          loop: "{{ shadowsocks_proxies | dict2items }}"

        - name: Create docker-compose.yml for Shadowsocks-Rust proxies
          ansible.builtin.template:
            src: templates/shadowsocks-compose.yml.j2
            dest: /srv/shadowsocks-rust/docker-compose.yml
            owner: "{{ ansible_user }}"
            group: "{{ deploy_group }}"
            mode: '0644'

        - name: Ensure old docker-compose.yaml file is removed to avoid conflicts
          ansible.builtin.file:
            path: /srv/shadowsocks-rust/docker-compose.yaml
            state: absent

        - name: Force stop and remove known proxy containers to prevent conflicts
          community.docker.docker_container:
            name: "{{ item.key }}"
            state: absent
          loop: "{{ shadowsocks_proxies | dict2items }}"
          loop_control:
            label: "{{ item.key }}"

        - name: Stop and remove any existing Shadowsocks-Rust proxy services
          community.docker.docker_compose_v2:
            project_src: "/srv/shadowsocks-rust"
            state: absent

        - name: Start Shadowsocks-Rust proxy services
          community.docker.docker_compose_v2:
            project_src: "/srv/shadowsocks-rust"
            state: present
            remove_orphans: true
            recreate: always
            pull: "{{ 'never' if fast_deploy | default(false) else 'missing' }}"
      when: shadowsocks_proxies is defined and shadowsocks_proxies | length > 0
      become: yes
