---
- name: 'Install required software'
  become: true
  ansible.builtin.apt:
    name: 'podman'
    state: 'present'

- name: 'Prepare uffd network'
  become: true
  containers.podman.podman_network:
    name: '{{ uffd_podman_network }}'
    ipv6: true
    state: 'quadlet'
  notify:
    - 'Reload uffd services'
    - 'Restart uffd network'

- name: 'Prepare uffd volumes'
  become: true
  containers.podman.podman_volume:
    name: '{{ item }}'
    state: 'quadlet'
  loop:
    - '{{ uffd_static_files_volume }}'
    - '{{ uffd_data_volume }}'
  notify:
    - 'Reload uffd services'
    - 'Restart uffd volumes'

- name: 'Create uffd config directory'
  become: true
  ansible.builtin.file:
    path: '{{ uffd_conf_dir }}'
    state: 'directory'
    mode: 'u=rwx,g=rx,o=rx'

- name: 'Generate uffd configuration'
  become: true
  ansible.builtin.template:
    src: 'uffd.cfg.j2'
    dest: '{{ uffd_conf_dir }}/uffd.cfg'
    owner: 'root'
    group: 'root'
    mode: 'u=rw,g=r,o=r'
  notify: 'Restart uffd container'

- name: 'Create uffd container'
  become: true
  containers.podman.podman_container:
    name: 'uffd-app'
    image: '{{ uffd_image_name }}'
    network: '{{ uffd_podman_network }}.network'
    state: 'quadlet'
    volume:
      - '{{ uffd_static_files_volume }}.volume:/var/www/uffd'
      - '{{ uffd_data_volume }}.volume:/var/lib/uffd'
      - '{{ uffd_conf_dir }}/uffd.cfg:/etc/uffd/uffd.cfg:ro'
    env:
      UFFD_INITIAL_ADMIN_USER: '{{ uffd_initial_admin_user }}'
      UFFD_INITIAL_ADMIN_PW: '{{ uffd_initial_admin_pw }}'
      UFFD_INITIAL_ADMIN_MAIL: '{{ uffd_initial_admin_mail }}'
    ports: []
    quadlet_options: |
      [Install]
      WantedBy=default.target
  notify:
    - 'Reload uffd services'
    - 'Restart uffd container'

- name: 'Define uffd-nginx image'
  become: true
  containers.podman.podman_image:
    name: '{{ uffd_nginx_image_name }}:{{ uffd_nginx_image_tag }}'
    quadlet_filename: 'uffd-nginx'
    state: 'quadlet'
  notify:
    - 'Reload uffd services'
    - 'Restart uffd-nginx image'

- name: 'Write uffd-nginx config'
  become: true
  ansible.builtin.template:
    src: 'nginx.conf.j2'
    dest: '{{ uffd_conf_dir }}/nginx.conf'
    owner: 'root'
    group: 'root'
    mode: 'u=rw,g=r,o=r'
  notify:
    - 'Restart uffd-nginx container'

- name: 'Create uffd-nginx container'
  become: true
  containers.podman.podman_container:
    name: 'uffd-nginx'
    image: 'uffd-nginx.image'
    network: '{{ uffd_podman_network }}.network'
    state: 'quadlet'
    volume:
      - '{{ uffd_static_files_volume }}.volume:/var/www/uffd'
      - '{{ uffd_conf_dir }}/nginx.conf:/etc/nginx/conf.d/default.conf:ro'
    ports: []
    quadlet_options: |
      [Install]
      WantedBy=default.target
      [Unit]
      Requires=uffd-app.service
      After=uffd-app.service
  notify:
    - 'Reload uffd services'
    - 'Restart uffd-nginx container'
