---
- name: Ensure configuration directory is present
  ansible.builtin.file:
    path: '/etc/gitea'
    state: directory
    mode: '0755'

- name: Ensure Quadlet files is present
  ansible.builtin.template:
    src: "{{ item }}.j2"
    dest: "/home/debian/.config/containers/systemd/{{ item }}"
    mode: '0755'
    owner: 'debian'
    group: 'debian'
  loop:
    - 'gitea-app.container'
    - 'gitea-postgres.container'

- name: Ensure Quadlet files is present
  ansible.builtin.copy:
    src: "{{ item }}"
    dest: "/home/debian/.config/containers/systemd/{{ item }}"
    mode: '0755'
    owner: 'debian'
    group: 'debian'
  loop:
    - 'gitea.pod'

- name: Ensure Caddyfile is present
  ansible.builtin.copy:
    src: 'gitea.Caddyfile'
    dest: '/etc/caddy/gitea.Caddyfile'
    owner: root
    group: root
    mode: '0644'
  notify: reload caddy

- name: Allow Gitea SSH connections
  community.general.ufw:
    port: 8016
    rule: 'allow'

# - name: Ensure compose file is present
#   ansible.builtin.copy:
#     src: 'compose.yml'
#     dest: '/etc/gitea/compose.yml'
#     mode: '0644'
#     owner: 'root'
#     group: 'root'
#   notify: 'restart gitea'

# - name: Ensure config file is present
#   ansible.builtin.template:
#     src: 'app.ini.j2'
#     dest: '/etc/gitea/app.ini'
#     mode: '0644'
#     owner: 'root'
#     group: 'root'
#   notify: 'restart gitea'

# - name: Ensure backup scripts are present
#   ansible.builtin.copy:
#     src: "gitea.{{ item }}.backup.sh"
#     dest: "/etc/backups/gitea.{{ item }}.backup.sh"
#     owner: 'root'
#     group: 'root'
#     mode: '0644'
#   loop:
#     - 'postgres'
#     - 'data'
#     - 'lfs'
#     - 'repositories'

# - name: systemd-reload
#   ansible.builtin.systemd_service:
#     daemon_reload: true
#   when: 'res.changed'

# - name: Ensure gitea service is enabled
#   ansible.builtin.service:
#     name: 'gitea'
#     enabled: true
