- name: Install and Configure Radicale
  block:
    - name: Install Radicale and dependencies via apt
      ansible.builtin.apt:
        name: [radicale, apache2-utils, git, python3]
        update_cache: true

    - name: Create radicale system user
      ansible.builtin.user:
        name: "{{ radicale_sys_user }}"
        system: true
        home: /
        shell: /usr/sbin/nologin
        create_home: true

    - name: Create Radicale config directory
      ansible.builtin.file:
        path: "{{ radicale_config_dir }}"
        state: directory
        owner: root
        group: root
        mode: "0755"

    - name: Create Radicale data directory
      ansible.builtin.file:
        path: "{{ radicale_data_dir }}"
        state: directory
        owner: "{{ radicale_sys_user }}"
        group: "{{ radicale_sys_user }}"
        mode: "0700"

    - name: Add Radicale admin user to htpasswd file
      no_log: true
      community.general.htpasswd:
        hash_scheme: "bcrypt"
        name: "{{ admin_user_name }}"
        password: "{{ radicale_password }}"
        path: "{{ radicale_users_file }}"
        owner: root
        group: "{{ radicale_sys_user }}"
        mode: "0640"

    - name: Download birthday calendar script
      ansible.builtin.get_url:
        url: https://raw.githubusercontent.com/iBigQ/radicale-birthday-calendar/main/create_birthday_calendar.py
        dest: /usr/local/bin/create_birthday_calendar.py
        owner: root
        group: root
        mode: "0755"

    - name: Fix shebang to use python3
      ansible.builtin.lineinfile:
        path: /usr/local/bin/create_birthday_calendar.py
        regexp: "^#!/usr/bin/env python$"
        line: "#!/usr/bin/env python3"
        state: present

    - name: Initialize git repository in collections directory
      become: true
      become_user: "{{ radicale_sys_user }}"
      ansible.builtin.command:
        cmd: git init
        chdir: "{{ radicale_data_dir }}"
        creates: "{{ radicale_data_dir }}/.git"
      tags:
        - skip_ansible_lint

    - name: Configure git user name
      become: true
      become_user: "{{ radicale_sys_user }}"
      community.general.git_config:
        name: user.name
        value: "{{ radicale_sys_user }}"
        repo: "{{ radicale_data_dir }}"
        scope: local

    - name: Configure git user email
      become: true
      become_user: "{{ radicale_sys_user }}"
      community.general.git_config:
        name: user.email
        value: "{{ radicale_sys_user }}@{{ ansible_hostname }}"
        repo: "{{ radicale_data_dir }}"
        scope: local

    - name: Create .gitignore for radicale
      ansible.builtin.copy:
        dest: "{{ radicale_data_dir }}/.gitignore"
        owner: "{{ radicale_sys_user }}"
        group: "{{ radicale_sys_user }}"
        mode: "0644"
        content: |
          .Radicale.cache
          .Radicale.lock
          .Radicale.tmp-*

    - name: Initial git commit
      become: true
      become_user: "{{ radicale_sys_user }}"
      ansible.builtin.shell: |
        git add .gitignore
        git diff --cached --quiet || git commit -m "Initial commit"
      args:
        chdir: "{{ radicale_data_dir }}"
      register: radicale_git_commit
      changed_when: "'Initial commit' in radicale_git_commit.stdout"
      failed_when: false
      tags:
        - skip_ansible_lint

    - name: Deploy Radicale configuration file
      ansible.builtin.template:
        src: templates/config.j2
        dest: "{{ radicale_config_dir }}/config"
        owner: root
        group: root
        mode: "0644"

    - name: Deploy Radicale systemd service file
      ansible.builtin.template:
        src: templates/radicale.service.j2
        dest: /etc/systemd/system/radicale.service
        owner: root
        group: root
        mode: "0644"

    - name: Copy Radicale Caddyfile to sites directory
      ansible.builtin.template:
        src: templates/Caddyfile.j2
        dest: "/etc/caddy/sites/{{ radicale_domain }}.caddyfile"
        owner: root
        group: root
        mode: "0644"
      notify: Restart caddy

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