- name: Install and Configure Caddy
  block:
    - name: Download xcaddy GPG key
      ansible.builtin.get_url:
        url: https://dl.cloudsmith.io/public/caddy/xcaddy/gpg.key
        dest: /usr/share/keyrings/caddy-xcaddy-archive-keyring.gpg
        mode: "0644"

    - name: Fetch xcaddy APT repository definition
      ansible.builtin.get_url:
        url: https://dl.cloudsmith.io/public/caddy/xcaddy/debian.deb.txt
        dest: /tmp/caddy-xcaddy.list.tmp
        mode: "0644"
      register: caddy_xcaddy_repo_url_content

    - name: Add xcaddy APT repository file
      ansible.builtin.copy:
        src: /tmp/caddy-xcaddy.list.tmp
        dest: /etc/apt/sources.list.d/caddy-xcaddy.list
        owner: root
        group: root
        mode: "0644"
        remote_src: true

    - name: Update apt cache after adding xcaddy repository
      ansible.builtin.apt:
        update_cache: true
        cache_valid_time: 0

    - name: Install xcaddy
      ansible.builtin.apt:
        name: xcaddy
        state: present

    - name: Check installed Caddy build id
      ansible.builtin.stat:
        path: "{{ caddy_build_id_file }}"
      register: caddy_build_id_stat

    - name: Read installed Caddy build id
      when: caddy_build_id_stat.stat.exists
      ansible.builtin.slurp:
        src: "{{ caddy_build_id_file }}"
      register: caddy_installed_build_id_raw

    - name: Set installed Caddy build id fact
      ansible.builtin.set_fact:
        caddy_installed_build_id: "{{ caddy_installed_build_id_raw.content | b64decode | trim if caddy_build_id_stat.stat.exists else '' }}"

    - name: Build Caddy with custom modules
      when: caddy_installed_build_id != caddy_build_id
      ansible.builtin.command: |
        xcaddy build \
          --with github.com/mholt/caddy-l4@{{ caddy_l4_version }} \
          --with github.com/caddy-dns/cloudflare@{{ caddy_cloudflare_plugin_version }} \
          --output {{ caddy_binary }}
      register: caddy_build_result
      changed_when: caddy_build_result.rc == 0
      notify: Restart caddy

    - name: Write installed Caddy build id
      when: caddy_installed_build_id != caddy_build_id
      ansible.builtin.copy:
        content: "{{ caddy_build_id }}"
        dest: "{{ caddy_build_id_file }}"
        owner: root
        group: root
        mode: "0644"

    - name: Create Caddy group
      ansible.builtin.group:
        name: caddy
        system: true
        state: present

    - name: Create Caddy user
      ansible.builtin.user:
        name: caddy
        group: caddy
        groups: www-data
        append: true
        system: true
        create_home: true
        home: /var/lib/caddy
        shell: /usr/sbin/nologin
        comment: Caddy web server
        state: present
      notify: Restart caddy

    - name: Create Caddy config and sites directories
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        owner: root
        group: root
        mode: "0755"
      loop: [/etc/caddy, /etc/caddy/sites]

    - name: Create Caddy log directory
      ansible.builtin.file:
        path: /var/log/caddy
        state: directory
        owner: caddy
        group: caddy
        mode: "0755"

    - name: Deploy Caddy systemd service file
      ansible.builtin.copy:
        src: files/caddy.service
        dest: /etc/systemd/system/caddy.service
        owner: root
        group: root
        mode: "0644"

    - name: Create Caddy systemd drop-in directory
      ansible.builtin.file:
        path: /etc/systemd/system/caddy.service.d
        state: directory
        owner: root
        group: root
        mode: "0755"

    - name: Deploy Caddy environment drop-in
      ansible.builtin.template:
        src: caddy-env.conf.j2
        dest: /etc/systemd/system/caddy.service.d/caddy-env.conf
        owner: root
        group: root
        mode: "0600"
      notify: Restart caddy

    - name: Copy Caddyfile from local path to /etc/caddy/Caddyfile
      ansible.builtin.copy:
        src: files/Caddyfile
        dest: /etc/caddy/Caddyfile
        owner: root
        group: root
        mode: "0644"
      notify: Restart caddy

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