---
- name: Ensure data directory is present
  ansible.builtin.file:
    path: '/mnt/data1/monica'
    state: directory
    mode: '0755'
    owner: 'root'
    group: 'root'

- name: Ensure data subvolumes are present
  community.general.btrfs_subvolume:
    name: '/monica/{{ item }}'
  with_items:
    - 'monica'
    - 'mariadb'

# Monica runs as www-data which is 33 in the container
- name: Ensure monica directory has correct permissions
  file:
    path: '/mnt/data1/monica/monica'
    state: directory
    mode: '0755'
    owner: 33
    group: 33

- name: Ensure configuration directory is present
  file:
    path: '/etc/monica'
    state: directory
    mode: '0755'

- name: Ensure compose file is present
  template:
    src: 'compose.yml.j2'
    dest: '/etc/monica/compose.yml'
    mode: '0644'
    owner: 'root'
    group: 'root'
  register: res

- name: Ensure stack is deployed
  ansible.builtin.shell:
    chdir: '/etc/monica'
    cmd: 'docker compose up -d --remove-orphans'
  when: 'res.changed'

- name: Ensure backup script is present
  ansible.builtin.copy:
    src: 'monica.backup.sh'
    dest: '/etc/backups/monica.backup.sh'
    owner: 'root'
    group: 'root'
    mode: '0644'
