- name: Install and configure Navidrome
  block:
    - name: Install required packages
      ansible.builtin.apt:
        name:
          - ffmpeg
        state: present
        update_cache: true

    - name: Fetch latest Navidrome release information from GitHub API
      become: false
      delegate_to: localhost
      register: navidrome_release_info
      ansible.builtin.uri:
        url: "{{ navidrome_github_api_latest_release_url }}"
        method: GET
        return_content: true

    - name: Set Navidrome version and download URL
      when: navidrome_release_info.json is defined
      delegate_to: localhost
      ansible.builtin.set_fact:
        navidrome_version: "{{ navidrome_release_info.json.tag_name | replace('v', '') }}"
        navidrome_deb_url: >-
          {{ (navidrome_release_info.json.assets |
              selectattr('name', 'search', 'navidrome_.*_linux_amd64.deb$') |
              map(attribute='browser_download_url') | first | default(''))
              if navidrome_release_info.json.assets | length > 0 else '' }}

    - name: Download Navidrome .deb package
      ansible.builtin.get_url:
        url: "{{ navidrome_deb_url }}"
        dest: "/tmp/navidrome_{{ navidrome_version }}_linux_amd64.deb"
        mode: "0644"
      when: navidrome_deb_url != ''

    - name: Install Navidrome from .deb package
      ansible.builtin.apt:
        deb: "/tmp/navidrome_{{ navidrome_version }}_linux_amd64.deb"
        state: present
      notify: Restart navidrome
      register: navidrome_install

    - name: Add ansible user to navidrome group for music uploads
      ansible.builtin.user:
        name: "{{ ansible_user }}"
        groups: "{{ navidrome_sys_group }}"
        append: true

    - name: Ensure music folder has correct permissions after install
      ansible.builtin.file:
        path: "{{ navidrome_music_folder }}"
        state: directory
        owner: "{{ navidrome_sys_user }}"
        group: "{{ navidrome_sys_group }}"
        mode: "2775"
        recurse: false

    - name: Set ownership for Navidrome data directory
      ansible.builtin.file:
        path: /var/lib/navidrome
        state: directory
        owner: "{{ navidrome_sys_user }}"
        group: "{{ navidrome_sys_group }}"
        mode: "0755"
        recurse: true

    - name: Deploy Navidrome configuration file
      ansible.builtin.template:
        src: templates/navidrome.toml.j2
        dest: "/etc/navidrome/navidrome.toml"
        owner: root
        group: root
        mode: "0644"
      notify: Restart navidrome

    - name: Enable and start Navidrome service
      ansible.builtin.systemd_service:
        name: navidrome
        enabled: true
        state: started
        daemon_reload: true

    - name: Deploy Navidrome Caddyfile
      ansible.builtin.template:
        src: templates/Caddyfile.j2
        dest: "/etc/caddy/sites/{{ navidrome_domain }}.caddyfile"
        owner: root
        group: root
        mode: "0644"
      notify: Restart caddy

    - name: Ensure Cloudflare DNS A record exists for {{ navidrome_subdomain + '.' + domain }}
      ansible.builtin.include_role:
        name: dns_record
      vars:
        dns_record_subdomain: "{{ navidrome_subdomain }}"
        dns_record_domain: "{{ domain }}"
        dns_record_cloudflare_api_token: "{{ cloudflare_dns_api_token }}"
