---
# Smoke tests for Veza V5 Ultra deployment
# Validates all services are running and accessible

- name: Run smoke tests for Veza deployment
  hosts: edge
  become: true
  gather_facts: true

  vars:
    test_timeout: 30
    retry_count: 5
    retry_delay: 10

  tasks:
    - name: Wait for all containers to be ready
      wait_for:
        timeout: "{{ test_timeout }}"
      delegate_to: localhost

    - name: Check container status
      command: incus list --format json
      register: container_status
      failed_when: false

    - name: Display container status
      debug:
        var: container_status.stdout
      when: container_status.stdout is defined

    - name: Test HAProxy container is running
      command: |
        incus exec veza-haproxy -- systemctl is-active haproxy
      register: haproxy_status
      failed_when: false

    - name: Test backend container is running
      command: |
        incus exec veza-backend -- systemctl is-active veza-backend
      register: backend_status
      failed_when: false

    - name: Test chat container is running
      command: |
        incus exec veza-chat -- systemctl is-active veza-chat
      register: chat_status
      failed_when: false

    - name: Test stream container is running
      command: |
        incus exec veza-stream -- systemctl is-active veza-stream
      register: stream_status
      failed_when: false

    - name: Test web container is running
      command: |
        incus exec veza-web -- systemctl is-active nginx
      register: web_status
      failed_when: false

    - name: Display service status
      debug:
        msg: |
          HAProxy: {{ haproxy_status.stdout }}
          Backend: {{ backend_status.stdout }}
          Chat: {{ chat_status.stdout }}
          Stream: {{ stream_status.stdout }}
          Web: {{ web_status.stdout }}

    - name: Test internal connectivity between containers
      command: |
        incus exec veza-backend -- curl -f http://veza-web:{{ veza_web_port }}/ || echo "Web container not reachable"
      register: internal_web_test
      failed_when: false

    - name: Test internal API connectivity
      command: |
        incus exec veza-web -- curl -f http://veza-backend:{{ veza_backend_port }}/health || echo "Backend API not reachable"
      register: internal_api_test
      failed_when: false

    - name: Test internal WebSocket connectivity
      command: |
        incus exec veza-web -- curl -f http://veza-chat:{{ veza_chat_port }}/ || echo "Chat server not reachable"
      register: internal_ws_test
      failed_when: false

    - name: Test internal stream connectivity
      command: |
        incus exec veza-web -- curl -f http://veza-stream:{{ veza_stream_port }}/ || echo "Stream server not reachable"
      register: internal_stream_test
      failed_when: false

    - name: Display internal connectivity test results
      debug:
        msg: |
          Internal Web: {{ internal_web_test.stdout }}
          Internal API: {{ internal_api_test.stdout }}
          Internal WS: {{ internal_ws_test.stdout }}
          Internal Stream: {{ internal_stream_test.stdout }}

    - name: Test external HTTP access (port 80)
      uri:
        url: "http://{{ ansible_host }}:80/"
        method: GET
        status_code: [200, 301, 302]
        timeout: "{{ test_timeout }}"
      register: http_test
      delegate_to: localhost
      retries: "{{ retry_count }}"
      delay: "{{ retry_delay }}"
      failed_when: false

    - name: Test external HTTPS access (port 443)
      uri:
        url: "https://{{ ansible_host }}:443/"
        method: GET
        status_code: [200, 301, 302]
        timeout: "{{ test_timeout }}"
        validate_certs: false
      register: https_test
      delegate_to: localhost
      retries: "{{ retry_count }}"
      delay: "{{ retry_delay }}"
      failed_when: false

    - name: Test API endpoint
      uri:
        url: "https://{{ ansible_host }}:443/api/health"
        method: GET
        status_code: [200, 404, 500]  # 404/500 might be expected if health endpoint not implemented
        timeout: "{{ test_timeout }}"
        validate_certs: false
      register: api_test
      delegate_to: localhost
      retries: "{{ retry_count }}"
      delay: "{{ retry_delay }}"
      failed_when: false

    - name: Test WebSocket endpoint (basic connectivity)
      uri:
        url: "https://{{ ansible_host }}:443/ws"
        method: GET
        status_code: [101, 200, 400, 404]  # 101 for successful WS upgrade
        timeout: "{{ test_timeout }}"
        validate_certs: false
      register: ws_test
      delegate_to: localhost
      retries: "{{ retry_count }}"
      delay: "{{ retry_delay }}"
      failed_when: false

    - name: Test stream endpoint
      uri:
        url: "https://{{ ansible_host }}:443/stream/"
        method: GET
        status_code: [200, 404, 500]  # 404/500 might be expected if no content
        timeout: "{{ test_timeout }}"
        validate_certs: false
      register: stream_test
      delegate_to: localhost
      retries: "{{ retry_count }}"
      delay: "{{ retry_delay }}"
      failed_when: false

    - name: Display external test results
      debug:
        msg: |
          HTTP (port 80): {{ http_test.status }} - {{ http_test.msg }}
          HTTPS (port 443): {{ https_test.status }} - {{ https_test.msg }}
          API (/api/health): {{ api_test.status }} - {{ api_test.msg }}
          WebSocket (/ws): {{ ws_test.status }} - {{ ws_test.msg }}
          Stream (/stream/): {{ stream_test.status }} - {{ stream_test.msg }}

    - name: Test HAProxy configuration
      command: |
        incus exec veza-haproxy -- haproxy -c -f /etc/haproxy/haproxy.cfg
      register: haproxy_config_test
      failed_when: false

    - name: Display HAProxy config test result
      debug:
        var: haproxy_config_test.stdout_lines
      when: haproxy_config_test.stdout_lines is defined

    - name: Check HAProxy logs for errors
      command: |
        incus exec veza-haproxy -- journalctl -u haproxy --no-pager -n 20
      register: haproxy_logs
      failed_when: false

    - name: Display HAProxy logs
      debug:
        var: haproxy_logs.stdout_lines
      when: haproxy_logs.stdout_lines is defined

    - name: Check application logs
      command: |
        incus exec {{ item.name }} -- journalctl -u {{ item.service }} --no-pager -n 10
      register: app_logs
      failed_when: false
      loop:
        - { name: "veza-backend", service: "veza-backend" }
        - { name: "veza-chat", service: "veza-chat" }
        - { name: "veza-stream", service: "veza-stream" }
        - { name: "veza-web", service: "nginx" }

    - name: Display application logs
      debug:
        var: app_logs.results

    - name: Test port accessibility
      wait_for:
        port: "{{ item }}"
        host: "{{ ansible_host }}"
        timeout: 10
      register: port_test
      delegate_to: localhost
      failed_when: false
      loop:
        - 80
        - 443

    - name: Display port test results
      debug:
        var: port_test.results

    - name: Final deployment summary
      debug:
        msg: |
          ========================================
          Veza V5 Ultra Deployment Summary
          ========================================
          Host: {{ ansible_host }}
          Domain: {{ domain }}
          
          Container Status:
          - HAProxy: {{ haproxy_status.stdout }}
          - Backend: {{ backend_status.stdout }}
          - Chat: {{ chat_status.stdout }}
          - Stream: {{ stream_status.stdout }}
          - Web: {{ web_status.stdout }}
          
          External Access:
          - HTTP: {{ http_test.status }}
          - HTTPS: {{ https_test.status }}
          - API: {{ api_test.status }}
          - WebSocket: {{ ws_test.status }}
          - Stream: {{ stream_test.status }}
          
          Next Steps:
          1. Point DNS A record for {{ domain }} to {{ ansible_host }}
          2. Re-run playbook 30-haproxy-in-container.yml to get Let's Encrypt cert
          3. Test full functionality with real domain
          ========================================

  handlers:
    - name: restart haproxy
      command: |
        incus exec veza-haproxy -- systemctl reload haproxy

    - name: restart backend
      command: |
        incus exec veza-backend -- systemctl restart veza-backend

    - name: restart chat
      command: |
        incus exec veza-chat -- systemctl restart veza-chat

    - name: restart stream
      command: |
        incus exec veza-stream -- systemctl restart veza-stream

    - name: restart web
      command: |
        incus exec veza-web -- systemctl restart nginx
