---
- name: 'Ensure repositories directory exists'
  file:
    path: "{{ repositories_path }}"
    state: directory
    owner: "{{ ansible_user }}"
  tags:
    - setup

- name: 'Create git user for ssh access'
  user:
    name: git
    shell: /bin/bash
    system: yes
    password: '!'
    state: present
    generate_ssh_key: yes
  register: git_user
  tags:
    - setup

- name: 'Add host key for gitea SSH passthrough'
  ansible.posix.authorized_key:
    user: "{{ git_user.name }}"
    key: "{{ git_user.ssh_public_key }}"
    state: present
    manage_dir: False
  tags:
    - setup

- name: 'Ensure /app/gitea exists'
  file:
    path: "/app/gitea"
    state: directory
  tags:
    - setup

- name: 'Copy gitea-ssh-forward script to remote'
  copy:
    src: "{{ service_path }}/bin/gitea-ssh-forward"
    dest: "/app/gitea/gitea"
    mode: +x
  tags:
    - setup