try:
    from google.adk.agents.context_cache_config import ContextCacheConfig
except (ImportError, AttributeError, ModuleNotFoundError):
    ContextCacheConfig = None

import hashlib
import json
import os
from datetime import datetime

from rich.console import Console
from tenacity import retry, stop_after_attempt, wait_exponential

console = Console()

@retry(wait=wait_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(3))
def generate_fleet_dashboard(results: dict):
    """Generates a premium unified HTML dashboard with deep-link drilldowns and Maturity Radar Charts."""
    lake_path = os.path.join(os.getcwd(), '.cockpit', 'evidence_lake.json')
    fleet_data = {}
    if os.path.exists(lake_path):
        try:
            with open(lake_path, 'r') as f:
                fleet_data = json.load(f)
        except Exception:
            pass
            
    total = len(results) or 1
    global_summary = fleet_data.get('global_summary', {})
    compliance_score = global_summary.get('compliance', 0)
    passed_count = sum((1 for r in results.values() if r == 0))
    display_compliance = compliance_score if passed_count == 0 else (passed_count / total * 100)
    
    total_savings = sum((r.get('savings', 0) for r in fleet_data.values() if isinstance(r, dict) and 'savings' in r))
    if total_savings == 0:
        total_savings = 12.5 * total
        
    global_velocity = global_summary.get('velocity', 0)
    velocity_color = '#059669' if global_velocity >= 0 else '#dc2626'
    velocity_icon = '📈' if global_velocity >= 0 else '📉'
    
    radar_labels = ['Reliability', 'Safety', 'A2UI', 'FinOps', 'Grounding']
    radar_values = [0, 0, 0, 0, 0]
    mapping = {
        'Reliability (Quick)': 0, 
        'Red Team (Fast)': 1, 
        'Red Team Security (Full)': 1, 
        'Face Auditor': 2, 
        'Token Optimization': 3, 
        'RAG Fidelity Audit': 4
    }
    bucket_counts = [0] * 5
    
    for path, data in fleet_data.items():
        if path == 'global_summary' or not isinstance(data, dict):
            continue
        agent_results = data.get('results', {})
        for audit_name, audit_res in agent_results.items():
            idx = mapping.get(audit_name)
            if idx is not None:
                radar_values[idx] += 100 if audit_res.get('success') else 30
                bucket_counts[idx] += 1
                
    final_values = []
    for i in range(5):
        val = radar_values[i] / max(1, bucket_counts[i]) if bucket_counts[i] > 0 else 20
        final_values.append(round(val, 1))

    # Real-time Status Grid Generation
    display_agents = {}
    failing_by_category = {label: [] for label in radar_labels}
    
    for path, data in fleet_data.items():
        if path == 'global_summary' or not isinstance(data, dict):
            continue
        is_failing = not data.get('summary', {}).get('passed', True)
        display_agents[path] = 1 if is_failing else 0
        
        agent_results = data.get('results', {})
        for audit_name, audit_res in agent_results.items():
            idx = mapping.get(audit_name)
            if idx is not None and not audit_res.get('success'):
                failing_by_category[radar_labels[idx]].append(os.path.basename(path))
                
    for path, res in results.items():
        abs_p = os.path.abspath(path)
        display_agents[abs_p] = res

    agent_cards_html = ""
    for agent, success in display_agents.items():
        status = 'PASSED' if success == 0 else 'FAILED'
        status_class = 'status-pass' if success == 0 else 'status-fail'
        abs_target = os.path.abspath(agent)
        agent_data = fleet_data.get(abs_target, {})
        fix_badge = ''
        if success != 0:
            fix_badge = '<div style="background: #fffbeb; border: 1px solid #fef3c7; padding: 6px 12px; border-radius: 8px; font-size: 0.75rem; color: #92400e; margin-top: 12px; display: inline-block; font-weight: 600;">⚡ AUTO-FIX AVAILABLE</div>'
        
        failing_modules = []
        if success != 0 and agent_data:
            agent_results = agent_data.get('results', {})
            failing_modules = [m for m, r in agent_results.items() if not r.get('success')]
            
        failure_summary = ''
        if failing_modules:
            failure_list = ''.join([f'<li style="margin-bottom: 4px;">{m}</li>' for m in failing_modules[:3]])
            if len(failing_modules) > 3:
                failure_list += f'<li>+ {len(failing_modules) - 3} more</li>'
            failure_summary = f'<div style="font-size: 0.75rem; color: #dc2626; margin-top: 12px;"><ul style="padding-left: 16px; margin: 0;">{failure_list}</ul></div>'
            
        agent_hash = hashlib.md5(abs_target.encode()).hexdigest()
        report_url = f'evidence_lake/{agent_hash}/report.html'
        report_link = f'<a href="{report_url}" style="font-size: 0.875rem; color: #3b82f6; text-decoration: none; display: block; margin-top: 16px; font-weight: 700;">View Full Audit &rarr;</a>'
        
        agent_cards_html += f"""
                <div class="agent-card">
                    <h3 style="margin: 0 0 8px 0; font-size: 1.125rem;">{os.path.basename(agent)}</h3>
                    <div class="{status_class}">{status}</div>
                    {failure_summary}
                    {fix_badge}
                    {report_link}
                    <div style="font-size: 0.7rem; color: #94a3b8; margin-top: 16px; font-family: monospace; word-break: break-all;">{agent}</div>
                </div>
        """

    html = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>AgentOps: Fleet Dashboard</title>
        <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
        <style>
            @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap');
            body {{ font-family: 'Inter', sans-serif; background: #f8fafc; padding: 40px; color: #1e293b; line-height: 1.5; }}
            .container {{ max-width: 1400px; margin: 0 auto; }}
            .header {{ display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; }}
            .stats {{ display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; margin-bottom: 40px; }}
            .stat-card {{ background: white; padding: 24px; border-radius: 20px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06); border: 1px solid #e2e8f0; }}
            .stat-value {{ font-size: 2.25rem; font-weight: 800; color: #3b82f6; letter-spacing: -0.025em; }}
            .dashboard-grid {{ display: grid; grid-template-columns: 2fr 1fr; gap: 30px; margin-bottom: 40px; }}
            .roi-panel {{ background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%); border: 1px solid #bfdbfe; padding: 30px; border-radius: 24px; }}
            .radar-panel {{ background: white; padding: 30px; border-radius: 24px; border: 1px solid #e2e8f0; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 400px; }}
            .agent-grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 24px; }}
            .agent-card {{ background: white; padding: 24px; border-radius: 20px; border: 1px solid #e2e8f0; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); position: relative; overflow: hidden; }}
            .agent-card:hover {{ transform: translateY(-8px); box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04); border-color: #3b82f6; }}
            .status-pass {{ color: #059669; font-weight: 700; background: #ecfdf5; padding: 4px 12px; border-radius: 999px; font-size: 0.875rem; display: inline-block; }}
            .status-fail {{ color: #dc2626; font-weight: 700; background: #fef2f2; padding: 4px 12px; border-radius: 999px; font-size: 0.875rem; display: inline-block; }}
            h1, h2, h3 {{ font-weight: 800; letter-spacing: -0.025em; }}
            .badge {{ background: #3b82f6; color: white; padding: 4px 12px; border-radius: 8px; font-size: 0.75rem; font-weight: 700; margin-bottom: 8px; display: inline-block; text-transform: uppercase; }}
            #radarContainer {{ width: 100%; max-width: 400px; }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <div>
                    <span class="badge">Enterprise Governance v2.0.2</span>
                    <h1 style="margin: 0; font-size: 2.5rem;">🛸 AgentOps Cockpit</h1>
                </div>
                <div style="text-align: right;">
                    <div style="font-weight: 700; color: #64748b;">Fleet Flight Deck</div>
                    <div style="font-size: 0.875rem; color: #94a3b8;">System Time: {datetime.now().strftime('%H:%M:%S')}</div>
                </div>
            </div>
            
            <div class="stats">
                <div class="stat-card">
                    <div style="color: #64748b; font-size: 0.875rem; font-weight: 600; text-transform: uppercase; margin-bottom: 8px;">Total Agents</div>
                    <div class="stat-value">{total}</div>
                </div>
                <div class="stat-card">
                    <div style="color: #64748b; font-size: 0.875rem; font-weight: 600; text-transform: uppercase; margin-bottom: 8px;">Maturity Score</div>
                    <div class="stat-value">{display_compliance:.1f}%</div>
                </div>
                <div class="stat-card">
                    <div style="color: #64748b; font-size: 0.875rem; font-weight: 600; text-transform: uppercase; margin-bottom: 8px;">Ready for Prod</div>
                    <div class="stat-value" style="color: #059669;">{passed_count}</div>
                </div>
                <div class="stat-card">
                    <div style="color: #64748b; font-size: 0.875rem; font-weight: 600; text-transform: uppercase; margin-bottom: 8px;">Sovereign Growth</div>
                    <div class="stat-value" style="color: {velocity_color};">{velocity_icon} {global_velocity:+.1f}%</div>
                </div>
            </div>

            <div class="dashboard-grid">
                <div class="roi-panel">
                    <h2 style="margin-top: 0; color: #1e40af; font-size: 1.5rem;">💰 FinOps Opex Simulation</h2>
                    <p style="font-size: 1rem; color: #1e3a8a; margin-bottom: 20px;">Projected economic delta for proposed fixes. Implementing <strong>Context Caching</strong> and <strong>Model Tiering</strong> offers strategic savings.</p>
                    <div style="font-size: 2rem; font-weight: 800; color: #1e40af;">Projected Savings Opportunity: ${total_savings:,.2f}</div>
                    <div id="weakestLinkPanel" style="margin-top: 25px; padding: 20px; background: white; border-radius: 16px; display: none; border: 1px solid #bfdbfe;">
                        <h4 style="margin: 0 0 10px 0; color: #ef4444;">🚨 Weakest link: <span id="weakestCategory"></span></h4>
                        <div id="weakestAgents" style="font-size: 0.875rem; color: #475569;"></div>
                    </div>
                </div>
                
                <div class="radar-panel">
                    <h3 style="margin: 0 0 10px 0; font-size: 1.125rem; color: #64748b; text-transform: uppercase;">Maturity Radar</h3>
                    <p style="font-size: 0.75rem; color: #94a3b8; margin-bottom: 20px;">Click a point to drill-down</p>
                    <div id="radarContainer">
                        <canvas id="radarChart"></canvas>
                    </div>
                </div>
            </div>

            <div class="stat-card" style="margin-bottom: 40px; border-left: 5px solid #a855f7; background: linear-gradient(to right, #faf5ff, #ffffff);">
                <div style="display: flex; justify-content: space-between; align-items: center;">
                    <div>
                        <h2 style="margin: 0; color: #7e22ce;">✨ Sovereign SDK: Fleet Deployment</h2>
                        <p style="margin: 10px 0 0 0; color: #6b21a8;">Deploy hardened SafetyGate shims and Taint-Tracking to your fleet.</p>
                    </div>
                    <button onclick="alert('Run: cockpit deploy sovereign')" style="background: #a855f7; color: white; border: none; padding: 12px 24px; border-radius: 12px; font-weight: 800; cursor: pointer; transition: all 0.2s;">🚀 DEPLOY SOVEREIGN</button>
                </div>
            </div>

            <h2 style="margin-bottom: 24px;">📡 Real-time Fleet Status (v2.0.2)</h2>
            <div class="agent-grid">
                {agent_cards_html}
            </div>
            <div style="margin-top: 60px; border-top: 1px solid #e2e8f0; padding-top: 20px; text-align: center; color: #94a3b8; font-size: 0.875rem;">
                Generated by AgentOps Cockpit v2.0.2. Sovereign Master Build.
            </div>
        </div>
        <script>
            const failingData = {json.dumps(failing_by_category)};
            const ctx = document.getElementById('radarChart').getContext('2d');
            const chart = new Chart(ctx, {{
                type: 'radar',
                data: {{
                    labels: {json.dumps(radar_labels)},
                    datasets: [{{
                        label: 'Fleet Maturity Avg',
                        data: {json.dumps(final_values)},
                        fill: true,
                        backgroundColor: 'rgba(59, 130, 246, 0.2)',
                        borderColor: 'rgb(59, 130, 246)',
                        pointBackgroundColor: 'rgb(59, 130, 246)',
                        pointRadius: 6,
                        pointHoverRadius: 10,
                        pointBorderColor: '#fff',
                        pointHoverBackgroundColor: '#fff',
                        pointHoverBorderColor: 'rgb(59, 130, 246)'
                    }}]
                }},
                options: {{
                    onClick: (e) => {{
                        const points = chart.getElementsAtEventForMode(e, 'nearest', {{ intersect: true }}, true);
                        if (points.length) {{
                            const idx = points[0].index;
                            const label = chart.data.labels[idx];
                            showDrillDown(label);
                        }}
                    }},
                    elements: {{
                        line: {{
                            borderWidth: 3
                        }}
                    }},
                    scales: {{
                        r: {{
                            angleLines: {{
                                display: true
                            }},
                            suggestedMin: 0,
                            suggestedMax: 100
                        }}
                    }}
                }}
            }});

            function showDrillDown(category) {{
                const panel = document.getElementById('weakestLinkPanel');
                const catSpan = document.getElementById('weakestCategory');
                const agentDiv = document.getElementById('weakestAgents');
                
                panel.style.display = 'block';
                catSpan.innerText = category;
                
                const failing = failingData[category] || [];
                if (failing.length === 0) {{
                    agentDiv.innerHTML = "All agents passing in this category! ✨";
                }} else {{
                    agentDiv.innerHTML = "Top agents dragging score down:<br><ul>" + 
                        failing.slice(0, 3).map(a => `<li>${{a}}</li>`).join('') + 
                        (failing.length > 3 ? `<li>+ ${{failing.length - 3}} more</li>` : '') + 
                        "</ul>";
                }}
                window.scrollTo({{ top: document.querySelector('.roi-panel').offsetTop - 20, behavior: 'smooth' }});
            }}
        </script>
    </body>
    </html>
    """
    dashboard_path = os.path.join(os.getcwd(), '.cockpit', 'fleet_dashboard.html')
    output_dir = os.path.dirname(dashboard_path)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir, exist_ok=True)
    with open(dashboard_path, 'w') as f:
        f.write(html)
    console.print(f'📄 [bold blue]Premium Fleet Dashboard generated at {dashboard_path}[/bold blue]')