# It isn't best practice to use this task
# Better load the repositories into /opt/docker/[servicename]/services, build them there and then use a docker-compose file for customizing
# @todo Refactor\Remove 
# @deprecated
- name: "Merge detached_files with applications['oauth2-proxy'].configuration_file"
  ansible.builtin.set_fact:
    merged_detached_files: "{{ detached_files + [applications['oauth2-proxy'].configuration_file] }}"
  when: applications[application_id].get('features', {}).get('oauth2', False) | bool

- name: "backup detached files"
  command: >
    mv "{{docker_compose.directories.instance}}{{ item }}" "/tmp/{{application_id}}-{{ item }}.backup"
  args:
    removes: "{{docker_compose.directories.instance}}{{ item }}"
  become: true
  loop: "{{ merged_detached_files | default(detached_files) }}"

- name: checkout repository 
  ansible.builtin.shell: git checkout .
  become: true
  args:
    chdir: "{{docker_compose.directories.instance}}"
  ignore_errors: true

# This could be replaced by include_role: docker-repository-setup
# Attendize and Akaunting still use this. When you refactor this code replace this.
- name: pull docker repository
  git:
    repo: "{{ docker_repository_address }}"
    dest: "{{ docker_repository_directory  | default(docker_compose.directories.instance) }}"
    update: yes
  notify: docker compose project setup
  become: true

- name: "restore detached files"
  command: >
    mv "/tmp/{{application_id}}-{{ item }}.backup" "{{docker_compose.directories.instance}}{{ item }}"
  args:
    removes: "/tmp/{{application_id}}-{{ item }}.backup"
  become: true
  loop: "{{ merged_detached_files | default(detached_files) }}"

- name: "copy {{ detached_files }} templates to server"
  template: 
    src: "{{ item }}.j2"
    dest: "{{docker_compose.directories.instance}}{{ item }}"
  loop: "{{ detached_files }}"
  notify: docker compose project setup
