---
# Datei: ansible/playbooks/deploy/includes/env_setup.yml
# Verwaltet die Erstellung und Konfiguration von Umgebungsvariablen

- name: Stelle sicher dass das Template-Verzeichnis existiert
  file:
    path: "{{ playbook_dir }}/../roles/deploy/templates"
    state: directory
    mode: '0755'
  delegate_to: localhost
  become: false
  run_once: true

- name: Erstelle .env-Template falls es nicht existiert
  copy:
    dest: "{{ playbook_dir }}/../roles/deploy/templates/.env.j2"
    content: |
      # Automatisch generierte .env-Datei
      # Generiert durch Ansible am {{ ansible_date_time.date }}

      COMPOSE_PROJECT_NAME={{ compose_project_name | default('michaelschiemer') }}

      # Allgemeine Einstellungen
      APP_NAME={{ app_name | default('michaelschiemer') }}
      APP_ENV={{ env_vars.APP_ENV | default('production') }}
      APP_DEBUG={{ env_vars.APP_DEBUG | default('false') }}
      APP_PORT={{ env_vars.APP_PORT | default(80) }}
      APP_SSL_PORT={{ env_vars.APP_SSL_PORT | default(443) }}

      # Server-Konfiguration
      APP_URL={{ 'https' if ssl_enabled | default(false) else 'http' }}://{{ app_domain }}
    mode: '0644'
  delegate_to: localhost
  become: false
  run_once: true

- name: .env-Datei erstellen oder aktualisieren
  template:
    src: ../roles/deploy/templates/.env.j2
    dest: "{{ deploy_root }}/.env"
    owner: "{{ deploy_user }}"
    group: "{{ deploy_user }}"
    mode: '0644'
  when: lookup('vars', 'env_vars', default=false)
