---
- name: Déployer Frontend Web
  hosts: edge
  become: true
  
  tasks:
    - name: Installer Node.js et nginx
      command: |
        incus exec veza-web -- bash -c 'apt update && apt install -y curl nginx'

    - name: Installer Node.js 18
      command: |
        incus exec veza-web -- bash -c '
          curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
          apt install -y nodejs
        '

    - name: Créer l'application web
      command: |
        incus exec veza-web -- bash -c 'cat > /var/www/html/index.html << EOF
        <!DOCTYPE html>
        <html lang="fr">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Veza V5 Ultra</title>
            <style>
                * { margin: 0; padding: 0; box-sizing: border-box; }
                body {
                    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    min-height: 100vh;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }
                .container {
                    background: white;
                    border-radius: 20px;
                    padding: 40px;
                    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
                    max-width: 600px;
                    width: 90%;
                }
                h1 {
                    color: #667eea;
                    font-size: 2.5rem;
                    margin-bottom: 10px;
                }
                .subtitle {
                    color: #666;
                    font-size: 1.1rem;
                    margin-bottom: 30px;
                }
                .status {
                    background: #10b981;
                    color: white;
                    padding: 15px;
                    border-radius: 10px;
                    margin-bottom: 30px;
                    font-weight: 600;
                }
                .services {
                    display: grid;
                    gap: 15px;
                }
                .service {
                    background: #f3f4f6;
                    padding: 15px;
                    border-radius: 10px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                }
                .service-name {
                    font-weight: 600;
                    color: #374151;
                }
                .service-status {
                    padding: 5px 15px;
                    border-radius: 20px;
                    font-size: 0.85rem;
                    font-weight: 600;
                }
                .online { background: #d1fae5; color: #065f46; }
                .checking { background: #fef3c7; color: #92400e; }
                .offline { background: #fee2e2; color: #991b1b; }
            </style>
        </head>
        <body>
            <div class="container">
                <h1>🎵 Veza V5 Ultra</h1>
                <div class="subtitle">Plateforme Audio Collaborative</div>
                <div class="status">✅ Système en Ligne</div>
                
                <div class="services">
                    <div class="service">
                        <span class="service-name">Backend API</span>
                        <span id="api-status" class="service-status checking">Vérification...</span>
                    </div>
                    <div class="service">
                        <span class="service-name">Chat WebSocket</span>
                        <span id="ws-status" class="service-status checking">Vérification...</span>
                    </div>
                    <div class="service">
                        <span class="service-name">Stream HLS</span>
                        <span id="stream-status" class="service-status checking">Vérification...</span>
                    </div>
                </div>
            </div>
            
            <script>
                // Test Backend API
                fetch("/api/health")
                    .then(r => r.json())
                    .then(d => {
                        const el = document.getElementById("api-status");
                        el.textContent = "✅ En Ligne";
                        el.className = "service-status online";
                    })
                    .catch(() => {
                        const el = document.getElementById("api-status");
                        el.textContent = "❌ Hors Ligne";
                        el.className = "service-status offline";
                    });

                // Test WebSocket (simulation)
                setTimeout(() => {
                    const el = document.getElementById("ws-status");
                    el.textContent = "⚠️ En Maintenance";
                    el.className = "service-status checking";
                }, 2000);

                // Test Stream (simulation)
                setTimeout(() => {
                    const el = document.getElementById("stream-status");
                    el.textContent = "⚠️ En Maintenance";
                    el.className = "service-status checking";
                }, 3000);
            </script>
        </body>
        </html>
        EOF'

    - name: Configurer nginx
      command: |
        incus exec veza-web -- bash -c 'cat > /etc/nginx/sites-available/default << EOF
        server {
            listen 3000 default_server;
            root /var/www/html;
            index index.html;
            
            location / {
                try_files \$uri \$uri/ =404;
            }
            
            location /api/ {
                proxy_pass http://10.20.0.101:8080;
                proxy_set_header Host \$host;
                proxy_set_header X-Real-IP \$remote_addr;
                proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto \$scheme;
            }
        }
        EOF'

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