<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multi-Cloud Orchestration Dashboard</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        :root {
            --primary: #667eea;
            --primary-dark: #5568d3;
            --success: #10b981;
            --warning: #f59e0b;
            --error: #ef4444;
            --info: #3b82f6;
            --bg: #f5f7fa;
            --card-bg: #ffffff;
            --text: #1f2937;
            --text-light: #6b7280;
            --border: #e5e7eb;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: var(--bg);
            color: var(--text);
            line-height: 1.6;
        }
        
        .navbar {
            background: linear-gradient(135deg, var(--primary) 0%, #764ba2 100%);
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .navbar h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }
        
        .navbar nav {
            display: flex;
            gap: 1.5rem;
        }
        
        .navbar a {
            color: white;
            text-decoration: none;
            opacity: 0.9;
            transition: opacity 0.2s;
        }
        
        .navbar a:hover {
            opacity: 1;
        }
        
        .container {
            max-width: 1600px;
            margin: 0 auto;
            padding: 2rem;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }
        
        .stat-card {
            background: var(--card-bg);
            padding: 1.5rem;
            border-radius: 12px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            border-left: 4px solid var(--primary);
            transition: transform 0.2s, box-shadow 0.2s;
        }
        
        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }
        
        .stat-card h3 {
            font-size: 0.875rem;
            color: var(--text-light);
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 0.5rem;
        }
        
        .stat-card .value {
            font-size: 2rem;
            font-weight: 700;
            color: var(--text);
        }
        
        .stat-card .change {
            font-size: 0.875rem;
            margin-top: 0.5rem;
        }
        
        .stat-card .change.positive {
            color: var(--success);
        }
        
        .stat-card .change.negative {
            color: var(--error);
        }
        
        .alerts-section {
            background: var(--card-bg);
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .alerts-section h2 {
            font-size: 1.25rem;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .alert-item {
            padding: 1rem;
            border-left: 4px solid;
            border-radius: 4px;
            margin-bottom: 0.5rem;
            background: var(--bg);
        }
        
        .alert-item.error {
            border-color: var(--error);
        }
        
        .alert-item.warning {
            border-color: var(--warning);
        }
        
        .alert-item.info {
            border-color: var(--info);
        }
        
        .provider-section {
            margin-bottom: 2rem;
        }
        
        .provider-header {
            background: var(--card-bg);
            padding: 1.25rem 1.5rem;
            border-radius: 12px 12px 0 0;
            border-bottom: 2px solid var(--primary);
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .provider-header h2 {
            color: var(--primary);
            font-size: 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .environments-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 1.5rem;
            padding: 1.5rem;
            background: var(--card-bg);
            border-radius: 0 0 12px 12px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .env-card {
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 1.5rem;
            transition: all 0.2s;
            background: var(--card-bg);
        }
        
        .env-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 16px rgba(0,0,0,0.1);
            border-color: var(--primary);
        }
        
        .env-card.enabled {
            border-left: 4px solid var(--success);
        }
        
        .env-card.disabled {
            border-left: 4px solid var(--text-light);
            opacity: 0.6;
        }
        
        .env-card-header {
            display: flex;
            justify-content: space-between;
            align-items: start;
            margin-bottom: 1rem;
        }
        
        .env-card h3 {
            color: var(--text);
            font-size: 1.125rem;
            margin-bottom: 0.25rem;
        }
        
        .env-card .meta {
            font-size: 0.875rem;
            color: var(--text-light);
            margin-bottom: 1rem;
        }
        
        .env-card .meta-item {
            margin-bottom: 0.25rem;
        }
        
        .status-badge {
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.375rem 0.75rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
            margin-top: 0.5rem;
        }
        
        .status-badge.healthy {
            background: #d1fae5;
            color: #065f46;
        }
        
        .status-badge.degraded {
            background: #fef3c7;
            color: #92400e;
        }
        
        .status-badge.unhealthy {
            background: #fee2e2;
            color: #991b1b;
        }
        
        .status-badge.deployed {
            background: #dbeafe;
            color: #1e40af;
        }
        
        .metrics-mini {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 0.75rem;
            margin-top: 1rem;
            padding-top: 1rem;
            border-top: 1px solid var(--border);
        }
        
        .metric-mini {
            text-align: center;
        }
        
        .metric-mini .label {
            font-size: 0.75rem;
            color: var(--text-light);
        }
        
        .metric-mini .value {
            font-size: 1.125rem;
            font-weight: 600;
            color: var(--text);
        }
        
        .env-actions {
            margin-top: 1rem;
            display: flex;
            gap: 0.5rem;
        }
        
        .btn {
            padding: 0.5rem 1rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.875rem;
            font-weight: 500;
            transition: all 0.2s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        
        .btn-primary:hover {
            background: var(--primary-dark);
        }
        
        .btn-secondary {
            background: var(--bg);
            color: var(--text);
            border: 1px solid var(--border);
        }
        
        .btn-secondary:hover {
            background: var(--border);
        }
        
        .recent-deployments {
            background: var(--card-bg);
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .recent-deployments h2 {
            font-size: 1.25rem;
            margin-bottom: 1rem;
        }
        
        .deployment-item {
            padding: 0.75rem;
            border-bottom: 1px solid var(--border);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .deployment-item:last-child {
            border-bottom: none;
        }
        
        .empty-state {
            text-align: center;
            padding: 3rem;
            color: var(--text-light);
        }
        
        .empty-state i {
            font-size: 3rem;
            margin-bottom: 1rem;
            opacity: 0.5;
        }
    </style>
</head>
<body>
    <div class="navbar">
        <h1><i class="fas fa-cloud"></i> Multi-Cloud Orchestration</h1>
        <nav>
            <a href="/"><i class="fas fa-home"></i> Dashboard</a>
            <a href="/dashboard/health"><i class="fas fa-heartbeat"></i> Health</a>
            <a href="/dashboard/costs"><i class="fas fa-dollar-sign"></i> Costs</a>
        </nav>
    </div>
    
    <div class="container">
        <!-- Statistics -->
        <div class="stats-grid">
            <div class="stat-card">
                <h3><i class="fas fa-server"></i> Total Environments</h3>
                        <div class="value"><%= total_environments %></div>
                <div class="change positive">+<%= enabled_count %> enabled</div>
            </div>
            <div class="stat-card">
                <h3><i class="fas fa-check-circle"></i> Enabled</h3>
                <div class="value"><%= enabled_count %></div>
                <div class="change"><%= total_environments > 0 ? ((enabled_count / total_environments * 100).toFixed(1)) : 0 %>% of total</div>
            </div>
            <div class="stat-card">
                <h3><i class="fas fa-cloud"></i> Providers</h3>
                <div class="value"><%= total_providers %></div>
                <div class="change">Multi-cloud</div>
            </div>
            <div class="stat-card">
                <h3><i class="fas fa-exclamation-triangle"></i> Active Alerts</h3>
                <div class="value"><%= alerts.length %></div>
                <div class="change <%= alerts.length > 0 ? 'negative' : 'positive' %>">
                    <%= alerts.length > 0 ? alerts.length + ' unacknowledged' : 'All clear' %>
                </div>
            </div>
        </div>
        
        <!-- Alerts Section -->
        <% if (alerts && alerts.length > 0) { %>
        <div class="alerts-section">
            <h2><i class="fas fa-bell"></i> Recent Alerts</h2>
            <% alerts.slice(0, 5).forEach(alert => { %>
            <div class="alert-item <%= alert.severity %>">
                <strong><%= alert.environment %></strong>: <%= alert.message %>
                <span style="float: right; color: var(--text-light); font-size: 0.875rem;">
                    <%= alert.timestamp.substring(0, 19) %>
                </span>
            </div>
            <% }); %>
        </div>
        <% } %>
        
        <!-- Recent Deployments -->
        <% if (recent_deployments && recent_deployments.length > 0) { %>
        <div class="recent-deployments">
            <h2><i class="fas fa-rocket"></i> Recent Deployments</h2>
            <% recent_deployments.forEach(deployment => { %>
            <div class="deployment-item">
                <div>
                    <strong><%= deployment.environment %></strong>
                    <span style="color: var(--text-light); font-size: 0.875rem; margin-left: 0.5rem;">
                        <%= (deployment.strategy || '').toUpperCase() %> - <%= deployment.version || 'N/A' %>
                    </span>
                </div>
                <span class="status-badge <%= deployment.status %>">
                    <%= (deployment.status || '').toUpperCase() %>
                </span>
            </div>
            <% }); %>
        </div>
        <% } %>
        
        <!-- Environments by Provider -->
        <% Object.keys(by_provider).forEach(provider => { %>
        <% const envs = by_provider[provider]; %>
        <div class="provider-section">
            <div class="provider-header">
                <h2>
                    <i class="fas fa-<%= provider === 'azure' ? 'microsoft' : provider === 'aws' ? 'aws' : provider === 'gcp' ? 'google' : 'server' %>"></i>
                    <%= provider.toUpperCase() %> Environments
                </h2>
                <span style="color: var(--text-light);"><%= envs.length %> environment(s)</span>
            </div>
            <div class="environments-grid">
                <% envs.forEach(env => { %>
                <div class="env-card <%= env.enabled ? 'enabled' : 'disabled' %>">
                    <div class="env-card-header">
                        <div>
                            <h3><%= env.name %></h3>
                            <div class="meta">
                                <div class="meta-item"><strong>Role:</strong> <%= (env.role || '').charAt(0).toUpperCase() + (env.role || '').slice(1) %></div>
                                <div class="meta-item"><strong>Region:</strong> <%= env.region %></div>
                                <div class="meta-item"><strong>Location:</strong> <%= env.location %></div>
                            </div>
                        </div>
                    </div>
                    
                    <% if (env.enabled && env_statuses[env.name]) { %>
                    <% const status = env_statuses[env.name]; %>
                    <div class="status-badge <%= status.cluster_health %>">
                        <i class="fas fa-circle" style="font-size: 0.5rem;"></i>
                        <%= (status.status || '').charAt(0).toUpperCase() + (status.status || '').slice(1) %>
                    </div>
                    
                    <div class="metrics-mini">
                        <div class="metric-mini">
                            <div class="label">CPU</div>
                            <div class="value"><%= status.cpu_usage_percent %>%</div>
                        </div>
                        <div class="metric-mini">
                            <div class="label">Memory</div>
                            <div class="value"><%= status.memory_usage_percent %>%</div>
                        </div>
                        <div class="metric-mini">
                            <div class="label">Nodes</div>
                            <div class="value"><%= status.node_count %></div>
                        </div>
                        <div class="metric-mini">
                            <div class="label">Pods</div>
                            <div class="value"><%= status.pods_running %>/<%= status.pods_total %></div>
                        </div>
                    </div>
                    <% } %>
                    
                    <div class="env-actions">
                        <a href="/environment/<%= env.name %>" class="btn btn-primary">
                            <i class="fas fa-eye"></i> View
                        </a>
                        <% if (env.enabled) { %>
                        <button class="btn btn-secondary" onclick="deploy('<%= env.name %>')">
                            <i class="fas fa-rocket"></i> Deploy
                        </button>
                        <% } %>
                    </div>
                </div>
                <% }); %>
            </div>
        </div>
        <% }); %>
    </div>
    
    <script>
        async function deploy(envName) {
            if (!confirm(`Deploy to ${envName}?`)) return;
            
            try {
                const response = await fetch(`/api/environments/${envName}/deploy`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        strategy: 'blue-green',
                        version: 'latest'
                    })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    alert(`✅ Deployment queued: ${data.deployment_id}`);
                    location.reload();
                } else {
                    alert(`❌ Error: ${data.error}`);
                }
            } catch (error) {
                alert(`❌ Error: ${error.message}`);
            }
        }
        
        // Auto-refresh every 30 seconds
        setInterval(() => {
            // Could implement partial refresh here
        }, 30000);
    </script>
</body>
</html>

