---
# Nur Dateien-Upload ohne Infrastruktur-Setup

- name: Upload Files Only to Netcup VPS
  hosts: all
  become: yes
  vars_files:
    - inventory/group_vars.yml
  
  tasks:
    - name: Check if app directory exists
      stat:
        path: "{{ app_directory }}"
      register: app_dir_exists
      
    - name: Create app directory if it doesn't exist
      file:
        path: "{{ app_directory }}"
        state: directory
        mode: '0755'
      when: not app_dir_exists.stat.exists
      
    - name: Check if docker-compose.yml exists locally
      local_action:
        module: stat
        path: "{{ local_app_path }}/docker-compose.yml"
      register: local_compose_exists
      become: no

    - name: Show upload information
      debug:
        msg: |
          📤 Uploading files...
          - From: {{ local_app_path }}
          - To: {{ app_directory }}
          - Docker Compose available: {{ local_compose_exists.stat.exists }}

    - name: Upload project files
      synchronize:
        src: "{{ local_app_path }}/"
        dest: "{{ app_directory }}/"
        delete: no
        archive: yes
        checksum: yes
        rsync_opts:
          - "--exclude=ansible"
          - "--exclude=.git"
          - "--exclude=vendor"
          - "--exclude=node_modules"
          - "--exclude=storage/logs"
          - "--exclude=cache"
          - "--exclude=logs"
          - "--exclude=dist"
          - "--exclude=.archive"
          - "--exclude=x_ansible"
          - "--exclude=.env"
          - "--verbose"
      register: sync_result

    - name: Ensure proper permissions for directories
      file:
        path: "{{ item }}"
        state: directory
        mode: '0755'
        recurse: yes
      loop:
        - "{{ app_directory }}/public"
        - "{{ app_directory }}/src"
        - "{{ app_directory }}/docker"
      ignore_errors: yes

    - name: Create storage directories if they don't exist
      file:
        path: "{{ app_directory }}/{{ item }}"
        state: directory
        mode: '0777'
      loop:
        - "storage/logs"
        - "storage/cache"
        - "cache"
        - "logs"
      ignore_errors: yes

    - name: Check if .env exists on server
      stat:
        path: "{{ app_directory }}/.env"
      register: server_env_exists

    - name: Create environment file if it doesn't exist
      template:
        src: roles/webapp/templates/app.env.j2
        dest: "{{ app_directory }}/.env"
      when: not server_env_exists.stat.exists
      register: env_created

    - name: Show what was uploaded
      debug:
        msg: |
          📂 Upload completed!
          
          📁 Files synced: {{ 'Yes' if sync_result.changed else 'No changes detected' }}
          📄 Environment file: {{ 'Created' if env_created.changed else 'Already exists' }}
          
          📍 Files are now at: {{ app_directory }}
          
          🔄 To restart the application, run:
          cd {{ app_directory }}
          docker compose down && docker compose up -d --build
          
          or use: make restart (if Makefile is available)

    - name: Check if containers are running (optional restart)
      shell: "cd {{ app_directory }} && docker compose ps --format json"
      register: container_status
      ignore_errors: yes
      changed_when: false

    - name: Ask about restarting containers
      debug:
        msg: |
          ℹ️  Current container status:
          {{ container_status.stdout if container_status.stdout else 'No containers found or docker compose not available' }}
          
          💡 Tip: If you want to restart the application with the new files, run:
          ansible-playbook restart-app.yml
          
          or manually:
          ssh {{ ansible_host }} "cd {{ app_directory }} && docker compose restart"
