---
# Update .env configuration without full redeploy
# Usage: ansible-playbook playbooks/configure.yml [--limit hostname]
# Regenerates .env + services.yaml and restarts API

- name: Reconfigure Changemaker Lite
  hosts: changemaker_instances
  become: true

  tasks:
    - name: Regenerate .env
      ansible.builtin.template:
        src: "{{ playbook_dir }}/../roles/changemaker/templates/env.j2"
        dest: "{{ cml_deploy_path }}/.env"
        mode: "0600"
        backup: true
      register: env_result

    - name: Regenerate Homepage services.yaml
      ansible.builtin.template:
        src: "{{ playbook_dir }}/../roles/changemaker/templates/services.yaml.j2"
        dest: "{{ cml_deploy_path }}/configs/homepage/services.yaml"
        mode: "0644"

    - name: Restart API to pick up new config
      ansible.builtin.command:
        cmd: docker compose restart api
        chdir: "{{ cml_deploy_path }}"
      when: env_result.changed
      changed_when: true

    - name: Configuration summary
      ansible.builtin.debug:
        msg: |
          Configuration {{ 'updated' if env_result.changed else 'unchanged' }} for {{ cml_domain }}
          {{ 'API restarted to apply changes' if env_result.changed else 'No restart needed' }}
