---
# Gemeinsame Tasks für alle Deployment-Szenarien

- name: Stelle sicher, dass Zielverzeichnisse existieren
  file:
    path: "{{ item }}"
    state: directory
    owner: "{{ deploy_user }}"
    group: "{{ deploy_user }}"
    mode: '0755'
  loop:
    - "{{ deploy_root }}"
    - "{{ deploy_root }}/public"
    - "{{ deploy_root }}/ssl"
    - "{{ deploy_root }}/src"
    - "{{ deploy_root }}/docker"
    - "{{ deploy_root }}/docker/nginx"
    - "{{ deploy_root }}/docker/php"

- name: SSL-Zertifikate prüfen
  stat:
    path: "/etc/letsencrypt/live/{{ app_domain }}/fullchain.pem"
  register: ssl_certs

- name: SSL-Zertifikate kopieren (falls vorhanden)
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    remote_src: yes
    owner: "{{ deploy_user }}"
    group: "{{ deploy_user }}"
    mode: '0644'
  loop:
    - { src: "/etc/letsencrypt/live/{{ app_domain }}/fullchain.pem", dest: "{{ deploy_root }}/ssl/fullchain.pem" }
    - { src: "/etc/letsencrypt/live/{{ app_domain }}/privkey.pem", dest: "{{ deploy_root }}/ssl/privkey.pem" }
  when: ssl_certs.stat.exists

- name: Kopiere Docker-Konfigurationen
  copy:
    src: "{{ playbook_dir }}/../docker/"
    dest: "{{ deploy_root }}/docker/"
    mode: '0644'

- name: Kopiere docker-compose.yml
  copy:
    src: "{{ playbook_dir }}/../docker-compose.yml"
    dest: "{{ deploy_root }}/docker-compose.yml"
    mode: '0644'

- name: Erstelle .env-Datei
  copy:
    dest: "{{ deploy_root }}/.env"
    content: |
      COMPOSE_PROJECT_NAME=michaelschiemer
      APP_ENV={{ environment | default('production') }}
      APP_DOMAIN={{ app_domain }}
      DEPLOY_ROOT={{ deploy_root }}
    owner: "{{ deploy_user }}"
    group: "{{ deploy_user }}"
    mode: '0644'

- name: Starte Docker-Container
  shell: |
    cd {{ deploy_root }} && \
    docker-compose up -d --build
  args:
    chdir: "{{ deploy_root }}"
  environment:
    COMPOSE_IGNORE_ORPHANS: "True"
    PATH: "/usr/local/bin:/usr/bin:/bin"

- name: Warte kurz bis Docker-Container gestartet sind
  pause:
    seconds: 5

- name: Erstelle Test-HTML-Datei
  copy:
    dest: "{{ deploy_root }}/public/index.html"
    content: |
      <!DOCTYPE html>
      <html>
      <head>
          <title>Server aktiv</title>
          <style>
              body { font-family: Arial, sans-serif; margin: 40px; }
              h1 { color: #3273dc; }
              .container { max-width: 800px; margin: 0 auto; padding: 20px; border: 1px solid #eee; border-radius: 5px; }
              .info { margin-top: 20px; background: #f8f8f8; padding: 10px; border-radius: 5px; }
          </style>
      </head>
      <body>
          <div class="container">
              <h1>Server ist aktiv!</h1>
              <p>Diese Seite bestätigt, dass der Webserver korrekt läuft.</p>
              <div class="info">
                  <p>Server: {{ inventory_hostname }}</p>
                  <p>Umgebung: {{ environment | default('unbekannt') }}</p>
                  <p>Deployment-Zeit: {{ ansible_date_time.iso8601 | default('unbekannt') }}</p>
              </div>
          </div>
      </body>
      </html>
    mode: '0644'
