---
# Playbook 08: Post-Deployment
# Final verification and configuration

- name: Post-Deployment Tasks
  hosts: all
  become: yes
  
  tasks:
    - name: Verify all containers are running
      command: docker compose ps --format json
      args:
        chdir: "{{ deployment_dir }}"
      register: container_status
      changed_when: false

    - name: Check Nextcloud status
      command: docker exec -u www-data next php occ status
      args:
        chdir: "{{ deployment_dir }}"
      register: nc_status
      changed_when: false
      ignore_errors: yes

    - name: Install recommended Nextcloud apps
      command: docker exec -u www-data next php occ app:install {{ item }}
      args:
        chdir: "{{ deployment_dir }}"
      loop:
        - calendar
        - contacts
        - tasks
        - notes
      ignore_errors: yes

    - name: Configure Nextcloud background jobs
      command: docker exec -u www-data next php occ background:cron
      args:
        chdir: "{{ deployment_dir }}"
      ignore_errors: yes

    - name: Setup Nextcloud cron job
      cron:
        name: "Nextcloud Background Jobs"
        minute: "*/5"
        job: "docker exec -u www-data next php /var/www/html/cron.php"
        user: root

    - name: Get Tailscale IP (if activated)
      command: tailscale ip -4
      register: tailscale_ip_result
      changed_when: false
      failed_when: false

    - name: Create deployment report
      copy:
        dest: "{{ deployment_dir }}/DEPLOYMENT.txt"
        content: |
          ════════════════════════════════════════════════════════════
          NEXTCLOUD STACK DEPLOYMENT REPORT
          ════════════════════════════════════════════════════════════
          
          Server: {{ inventory_hostname }}
          IP Address: {{ ansible_host }}
          Domain: {{ domain }}
          Deployment Date: {{ ansible_date_time.iso8601 }}
          
          ────────────────────────────────────────────────────────────
          PUBLIC SERVICES (HTTPS)
          ────────────────────────────────────────────────────────────
          • Nextcloud:    https://{{ subdomain_nextcloud }}.{{ domain }}
          • OnlyOffice:   https://{{ subdomain_office }}.{{ domain }}
          • Excalidraw:   https://{{ subdomain_draw }}.{{ domain }}
          • Obsidian:     https://{{ subdomain_notes }}.{{ domain }}
          
          ────────────────────────────────────────────────────────────
          MANAGEMENT SERVICES (Tailscale only)
          ────────────────────────────────────────────────────────────
          • Homarr:        https://{{ subdomain_homarr }}.{{ domain }}
          • Dockhand:      https://{{ subdomain_dockhand }}.{{ domain }}
          • Uptime Kuma:   https://{{ subdomain_uptime }}.{{ domain }}
          
          ────────────────────────────────────────────────────────────
          CREDENTIALS
          ────────────────────────────────────────────────────────────
          Nextcloud Admin User: {{ admin_user }}
          Nextcloud Admin Password: [stored in Ansible vault]
          
          ────────────────────────────────────────────────────────────
          TAILSCALE
          ────────────────────────────────────────────────────────────
          {% if tailscale_ip_result.rc == 0 %}
          Status: Connected
          Tailscale IP: {{ tailscale_ip_result.stdout }}
          {% else %}
          Status: Not activated
          Activate with: sudo tailscale up
          {% endif %}
          
          ────────────────────────────────────────────────────────────
          BACKUPS
          ────────────────────────────────────────────────────────────
          Schedule: Daily at 3:00 AM
          Location: {{ deployment_dir }}/backups/
          Retention: {{ backup_retention_days }} days
          
          ────────────────────────────────────────────────────────────
          USEFUL COMMANDS
          ────────────────────────────────────────────────────────────
          View containers:    cd {{ deployment_dir }} && docker compose ps
          View logs:          cd {{ deployment_dir }} && docker compose logs -f
          Restart service:    cd {{ deployment_dir }} && docker compose restart [service]
          Manual backup:      {{ deployment_dir }}/backup.sh
          Nextcloud CLI:      docker exec -u www-data next php occ [command]
          
          ════════════════════════════════════════════════════════════

    - name: Display deployment summary
      debug:
        msg: |
          ╔════════════════════════════════════════════════════════════╗
          ║          DEPLOYMENT COMPLETED SUCCESSFULLY!                ║
          ╚════════════════════════════════════════════════════════════╝
          
          Server: {{ inventory_hostname }} ({{ ansible_host }})
          Domain: {{ domain }}
          
          📦 Public Services:
            • Nextcloud:  https://{{ subdomain_nextcloud }}.{{ domain }}
            • OnlyOffice: https://{{ subdomain_office }}.{{ domain }}
            • Excalidraw: https://{{ subdomain_draw }}.{{ domain }}
            • Obsidian:   https://{{ subdomain_notes }}.{{ domain }}
          
          🔒 Management (Tailscale only):
            • Homarr:       https://{{ subdomain_homarr }}.{{ domain }}
            • Dockhand:     https://{{ subdomain_dockhand }}.{{ domain }}
            • Uptime Kuma:  https://{{ subdomain_uptime }}.{{ domain }}
          
          👤 Nextcloud Admin:
            Username: {{ admin_user }}
            Password: [check vault]
          
          💾 Backups:
            Daily at 3:00 AM
            Location: {{ deployment_dir }}/backups/
          
          📝 Next Steps:
            1. Login to Nextcloud and complete setup
            2. Setup Uptime Kuma monitoring (via Tailscale)
            3. Configure Homarr dashboard (via Tailscale)
            4. Review deployment report: {{ deployment_dir }}/DEPLOYMENT.txt
          
          Deployment report saved to:
          {{ deployment_dir }}/DEPLOYMENT.txt
