---
# Playbook 05: Deploy Stack
# Deploy Docker Compose stack with all services

- name: Deploy Nextcloud Stack
  hosts: all
  become: yes
  
  tasks:
    - name: Create directory structure
      file:
        path: "{{ item }}"
        state: directory
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: '0755'
      loop:
        - "{{ deployment_dir }}/configs"
        - "{{ deployment_dir }}/configs/caddy"
        - "{{ deployment_dir }}/configs/nextcloud"
        - "{{ deployment_dir }}/data"
        - "{{ deployment_dir }}/data/homarr"
        - "{{ deployment_dir }}/data/obsidian/config"
        - "{{ deployment_dir }}/data/obsidian/vault"
        - "{{ deployment_dir }}/backups"
        - "{{ deployment_dir }}/backups/database"
        - "{{ deployment_dir }}/backups/volumes"

    - name: Template docker-compose.yml
      template:
        src: ../roles/nextcloud_stack/templates/docker-compose.yml.j2
        dest: "{{ deployment_dir }}/docker-compose.yml"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: '0644'

    - name: Template .env file
      template:
        src: ../roles/nextcloud_stack/templates/env.j2
        dest: "{{ deployment_dir }}/.env"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: '0600'
      no_log: true

    - name: Template Caddyfile
      template:
        src: ../roles/caddy/templates/Caddyfile.j2
        dest: "{{ deployment_dir }}/configs/caddy/Caddyfile"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: '0644'

    - name: Pull Docker images
      command: docker compose pull
      args:
        chdir: "{{ deployment_dir }}"
      become_user: "{{ ansible_user }}"

    - name: Start Docker Compose stack
      command: docker compose up -d
      args:
        chdir: "{{ deployment_dir }}"
      become_user: "{{ ansible_user }}"

    - name: Wait for PostgreSQL to be healthy
      command: docker exec next-db pg_isready -U {{ db_user }}
      register: pg_ready
      until: pg_ready.rc == 0
      retries: 30
      delay: 10
      changed_when: false

    - name: Wait for Nextcloud to be accessible
      uri:
        url: http://localhost:8808/status.php
        status_code: 200
      register: nc_status
      until: nc_status.status == 200
      retries: 30
      delay: 10

    - name: Configure Nextcloud Redis cache
      command: |
        docker exec -u www-data next php occ config:system:set redis host --value=next-redis
      args:
        chdir: "{{ deployment_dir }}"
      ignore_errors: yes

    - name: Configure Nextcloud Redis port
      command: |
        docker exec -u www-data next php occ config:system:set redis port --value=6379
      args:
        chdir: "{{ deployment_dir }}"
      ignore_errors: yes

    - name: Configure Nextcloud memcache
      command: |
        docker exec -u www-data next php occ config:system:set memcache.locking --value='\\OC\\Memcache\\Redis'
      args:
        chdir: "{{ deployment_dir }}"
      ignore_errors: yes

    - name: Display deployment success
      debug:
        msg: |
          ✓ Docker Compose stack deployed
          ✓ All containers started
          ✓ Nextcloud is accessible
