---
- name: "load docker, db and proxy for {{ application_id }}"
  include_role: 
    name: cmp-db-docker-proxy
  vars:
    docker_compose_flush_handlers:  true

- name: Check if config.php exists in EspoCRM
  command: docker exec --user root {{ espocrm_name }} test -f {{ espocrm_config_file }}
  register: config_file_exists
  changed_when: false
  failed_when: false

- name: Patch EspoCRM config.php with updated DB credentials
  include_tasks: 01_patch_config.yml
  when: config_file_exists.rc == 0

- name: Flush handlers to make DB available before password reset
  meta: flush_handlers

- name: Set OIDC scopes in EspoCRM config (inside web container)
  ansible.builtin.shell: |
    docker compose exec -T web php -r '
      require "/var/www/html/bootstrap.php";
      $writer = (new \Espo\Core\Application())
        ->getContainer()
        ->get("injectableFactory")
        ->create("\Espo\Core\Utils\Config\ConfigWriter");
      $writer->set("oidcScopes", ["openid", "profile", "email"]);
      $writer->save();
    '
  args:
    chdir: "{{ docker_compose.directories.instance }}"