---
- name: Load bridges configuration
  include_vars:
    file: "bridges.yml"

- name: Filter enabled bridges and register as fact
  set_fact:
    bridges: "{{ bridges_configuration | filter_enabled_bridges(applications[application_id].plugins) }}"
  changed_when: false

- name: "include docker-central-database"
  include_role: 
    name: docker-central-database

- name: "include role for {{application_id}} to receive certs & do modification routines"
  include_role:
    name: nginx-https-get-cert-modify-all
  vars:
    domain:     "{{domains.matrix.synapse}}"
    http_port:  "{{ports.localhost.http.synapse}}"

- name: create {{well_known_directory}}
  file:
    path: "{{well_known_directory}}"
    state: directory
    mode: '0755'

- name: create {{well_known_directory}}server
  template: 
    src:  "well-known.j2" 
    dest: "{{well_known_directory}}server"

- name: create {{domains.matrix.synapse}}.conf
  template: 
    src:  "templates/nginx.conf.j2" 
    dest: "{{nginx.directories.http.servers}}{{domains.matrix.synapse}}.conf"
  vars:
    domain:     "{{domains.matrix.synapse}}" # Didn't work in the past. May it works now. This does not seem to work @todo Check how to solve without declaring set_fact, seems a bug at templates
    http_port:  "{{ports.localhost.http.synapse}}"
  notify: restart nginx
        
- name: "include role nginx-domain-setup for {{application_id}}"
  include_role: 
    name: nginx-domain-setup
  vars:        
    domain:     "{{domains.matrix.element}}"
    http_port:  "{{ports.localhost.http.element}}"

- name: include create-and-seed-database.yml for multiple bridges
  include_tasks: create-and-seed-database.yml
  vars:
    database_password:  "{{ item.database_password }}"
    database_username:  "{{ item.database_username }}"
    database_name:      "{{ item.database_name }}"
  loop: "{{ bridges }}"

# The following taks are necessary because a clean setup is necessary
- name: shut down docker compose project 
  command:
    cmd: docker-compose -p "{{application_id}}" down
    chdir: "{{ docker_compose.directories.instance }}"

- name: "cleanup project folder"
  file:
    path: "{{docker_compose.directories.instance}}mautrix/"
    state: absent

- name: "create bridge folders"
  file:
    path: "{{docker_compose.directories.instance}}mautrix/{{item.bridge_name}}"
    state: directory
    mode: 0755
  loop: "{{ bridges }}"

- name: add multiple mautrix bridge configuration
  template: 
    src:  "mautrix/{{item.bridge_name}}.config.yml.j2" 
    dest: "{{docker_compose.directories.instance}}mautrix/{{item.bridge_name}}/config.yaml"
  loop: "{{ bridges }}"
  notify: docker compose project setup

- name: add element configuration
  template: 
    src:  "element.config.json.j2" 
    dest: "{{docker_compose.directories.instance}}element-config.json"
  notify: docker compose project setup

- name: add synapse homeserver configuration
  template: 
    src:  "synapse/homeserver.yaml.j2" 
    dest: "{{docker_compose.directories.instance}}homeserver.yaml"
  notify: docker compose project setup

- name: add synapse log configuration
  template: 
    src:  "synapse/log.config.j2" 
    dest: "{{docker_compose.directories.instance}}{{domains.matrix.synapse}}.log.config"
  notify: docker compose project setup

# https://github.com/matrix-org/synapse/issues/6303
- name: set correct folder permissions
  command:
    cmd: "docker run --rm --mount type=volume,src=matrix_synapse_data,dst=/data -e SYNAPSE_SERVER_NAME={{domains.matrix.synapse}} -e SYNAPSE_REPORT_STATS=no --entrypoint /bin/sh matrixdotorg/synapse:latest -c 'chown -vR 991:991 /data'"

- name: add docker-compose.yml
  template: 
    src:  "docker-compose.yml.j2" 
    dest: "{{docker_compose.directories.instance}}docker-compose.yml"
  notify: docker compose project setup

# Pull image when update is wished. 
# @todo This should be moved to update-docker
- name: docker compose pull
  command:
    cmd: docker-compose -p "{{application_id}}" pull
    chdir: "{{docker_compose.directories.instance}}"
  when: mode_update | bool

- name: docker compose project setup
  command:
    cmd: "docker-compose -p {{application_id}} up -d --remove-orphans"
    chdir: "{{docker_compose.directories.instance}}"
  environment:
    COMPOSE_HTTP_TIMEOUT:   600
    DOCKER_CLIENT_TIMEOUT:  600
  register: result
  until:    result is succeeded
  retries:  12
  delay:    30

- name: wait for registration files
  wait_for:
    path: "{{docker_compose.directories.instance}}mautrix/{{item.bridge_name}}/registration.yaml"
    state: present  
    timeout: 120
  loop: "{{ bridges }}"

- name: "change file permissions"
  file:
    path: "{{docker_compose.directories.instance}}mautrix/{{item.bridge_name}}/registration.yaml"
    mode: 0755
  loop: "{{ bridges }}"

- name: create admin account
  command:
    cmd: docker compose exec -it synapse register_new_matrix_user -u {{applications[application_id].users.administrator.username}} -p {{applications[application_id].credentials.administrator_password}} -a -c /data/homeserver.yaml http://localhost:8008
    chdir: "{{ docker_compose.directories.instance }}"
  ignore_errors: true
  when: applications[application_id].setup | bool

- name: create chatgpt bot
  command:
    cmd: docker compose exec -it synapse register_new_matrix_user -u chatgptbot -p {{applications[application_id].credentials.chatgpt_bridge_user_password}} -a -c /data/homeserver.yaml http://localhost:8008
    chdir: "{{ docker_compose.directories.instance }}"
  ignore_errors: true
  when: applications[application_id].setup | bool