---
- name: Create Gitea data directory
  ansible.builtin.file:
    path: "{{ gitea_data_dir }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  tags: gitea

# Gitea runs as git (UID 1000) and writes directly to this directory
- name: Create Gitea app data directory (git UID 1000)
  ansible.builtin.file:
    path: "{{ gitea_data_dir }}/data"
    state: directory
    owner: "1000"
    group: "1000"
    mode: "0755"
  tags: gitea

- name: Create Gitea database directory (postgres UID 999)
  ansible.builtin.file:
    path: "{{ gitea_data_dir }}/db"
    state: directory
    owner: "999"
    group: "999"
    mode: "0700"
  tags: gitea

# Git-over-SSH runs on 2222 to avoid conflict with the host SSH on 22
- name: Allow Gitea SSH (2222/tcp)
  community.general.ufw:
    rule: allow
    port: "2222"
    proto: tcp
  tags: gitea

- name: Deploy Docker Compose file
  ansible.builtin.template:
    src: docker-compose.yml.j2
    dest: "{{ gitea_data_dir }}/docker-compose.yml"
    owner: root
    group: root
    mode: "0644"
  notify: Restart gitea
  tags: gitea

- name: Start Gitea
  community.docker.docker_compose_v2:
    project_src: "{{ gitea_data_dir }}"
    state: present
    pull: missing
  tags: gitea
