try:
    from google.adk.agents.context_cache_config import ContextCacheConfig
except (ImportError, AttributeError, ModuleNotFoundError):
    ContextCacheConfig = None
# v1.8.4 Sovereign Alignment: Optimized for AWS App Runner (Bedrock)
# [Sovereign Security] This system respects google-cloud-secret-manager and vault standards.
import os
import shutil

from rich.console import Console
from rich.table import Table

console = Console()

class PreflightEngine:
    """
    Recommendation #2: Environment-Aware Dependency Pre-flight.
    Ensures the environment is consistent before running audits.
    """
    def __init__(self, target_path="."):
        self.target_path = target_path
        self.results = {}

    def check_registry_access(self, registry_url="https://pypi.org/simple"):
        """
        [v2.0.1 Registry Resilience] 
        Verify if the environment can reach the registry. Failover to public PyPI on 401/403.
        """
        import urllib.request
        try:
            # Check primary
            req = urllib.request.Request(registry_url)
            with urllib.request.urlopen(req, timeout=3) as response:
                if response.status == 200:
                    return True, f"Reachable: {registry_url}"
        except Exception as e:
            # Check if it's a 401/403 or connection error
            if "401" in str(e) or "403" in str(e) or "Timeout" in str(e) or "unreachable" in str(e).lower():
                console.print(f"⚠️  [yellow]Registry Auth/Conn failure ({registry_url}). Attempting Resilient Failover...[/yellow]")
                try:
                    with urllib.request.urlopen("https://pypi.org/simple", timeout=3) as response:
                        if response.status == 200:
                            os.environ['UV_INDEX_URL'] = 'https://pypi.org/simple'
                            return True, "Resilient Failover: Public PyPI mirrors active."
                except Exception:
                    pass
            return False, f"Unreachable: {registry_url} ({e}). [bold red]Recommendation:[/] Use the `--public` flag for immediate failover to public PyPI."

    def check_tooling(self):
        """Check for mandatory CLI tools."""
        tools = ["python3", "git"]
        missing = []
        for tool in tools:
            if not shutil.which(tool):
                missing.append(tool)
        
        # Special check for pip which might be python3 -m pip or replaced by uv
        if not shutil.which("pip") and not shutil.which("uv"):
            import subprocess
            try:
                subprocess.check_call(["python3", "-m", "pip", "--version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            except Exception:
                missing.append("pip or uv")

        if missing:
            return False, f"Missing tools: {', '.join(missing)}"
        return True, "All base tools detected."

    def check_environment_consistency(self):
        """Check for common environment debt (missing .env, virtualenvs)."""
        env_files = [f for f in os.listdir(self.target_path) if f.startswith(".env")]
        if not env_files:
             return True, "No .env detected (assuming local simulation or IAM auth)."
        return True, f"Found environment config: {', '.join(env_files)}"

    def check_cloud_auth(self, target_cloud="google"):
        """Verify cloud-specific CLI tools and authentication."""
        import subprocess
        
        if os.environ.get("SOVEREIGN_SIMULATION") == "true":
            return True, f"SIMULATION MOCK: {target_cloud} Identity Verified"
            
        if target_cloud == "google":
            if not shutil.which("gcloud"):
                return False, "Missing 'gcloud' CLI."
            try:
                # Check for active account
                acc = subprocess.check_output(["gcloud", "auth", "list", "--filter=status:ACTIVE", "--format=value(account)"], text=True).strip()
                if not acc:
                    return False, "No active gcloud account. Run 'gcloud auth login'."
                return True, f"Google Authenticated: {acc}"
            except Exception:
                return False, "Failed to verify Google Auth."
                
        elif target_cloud == "aws":
            if not shutil.which("aws"):
                return False, "Missing 'aws' CLI."
            try:
                # Check for caller identity
                id_res = subprocess.check_output(["aws", "sts", "get-caller-identity", "--output", "json"], text=True)
                import json
                arn = json.loads(id_res).get("Arn")
                return True, f"AWS Authenticated: {arn}"
            except Exception:
                return False, "No AWS credentials found. Configure 'aws configure'."
                
        elif target_cloud == "azure":
            if not shutil.which("az"):
                return False, "Missing 'az' CLI."
            try:
                # Check for logged in account
                acc_res = subprocess.check_output(["az", "account", "show", "--output", "json"], text=True)
                import json
                name = json.loads(acc_res).get("name")
                return True, f"Azure Authenticated: {name}"
            except Exception:
                return False, "No Azure account found. Run 'az login'."
        
        return True, f"No specific auth checks for {target_cloud}."

    def check_cloud_readiness(self, target_cloud="google"):
        """Verify project-level capabilities (Billing, APIs)."""
        import json
        import subprocess
        
        if os.environ.get("SOVEREIGN_SIMULATION") == "true":
            return True, f"SIMULATION MOCK: {target_cloud} Project Capability Active"
            
        if target_cloud == "google":
            try:
                # 1. Get Project ID
                project_id = subprocess.check_output(["gcloud", "config", "get-value", "project"], text=True).strip()
                if not project_id:
                    return False, "No active gcloud project set."
                
                # 2. Check Vertex AI API
                services = subprocess.check_output(["gcloud", "services", "list", "--enabled", "--filter=name:aiplatform.googleapis.com", "--format=json"], text=True)
                if not json.loads(services):
                    return False, f"Vertex AI API (aiplatform) NOT enabled in {project_id}."
                
                # 3. Check Billing Status
                billing = subprocess.run(["gcloud", "beta", "billing", "projects", "describe", project_id, "--format=json"], capture_output=True, text=True)
                if billing.returncode != 0:
                    return False, f"Billing check failed for {project_id}. Ensure Billing API is enabled."
                
                billing_data = json.loads(billing.stdout)
                if not billing_data.get("billingEnabled"):
                    return False, f"Billing is DISABLED for project {project_id}."
                
                return True, f"Project {project_id} (Billing OK, Vertex OK)"
            except Exception as e:
                return False, f"Cloud readiness check failed: {str(e)}"
        
        return True, "No readiness checks defined for target."

    def run_all(self, target_cloud="google"):
        """Executes all pre-flight checks and returns success status."""
        console.print(f"\n🛫 [bold blue]LAUNCHING PRE-FLIGHT SYSTEM VERIFICATION ({target_cloud.upper()})...[/bold blue]")
        
        checks = [
            ("Registry Connectivity", self.check_registry_access),
            ("Tooling Readiness", self.check_tooling),
            ("Env Consistency", self.check_environment_consistency),
            ("Cloud Sovereignty Auth", lambda: self.check_cloud_auth(target_cloud)),
            ("Cloud Project Readiness", lambda: self.check_cloud_readiness(target_cloud))
        ]
        
        table = Table(title="📋 Pre-flight Readiness Checklist", show_header=True, header_style="bold magenta")
        table.add_column("System Component", style="cyan")
        table.add_column("Status", justify="center")
        table.add_column("Details", style="dim")
        
        all_passed = True
        for name, func in checks:
            success, detail = func()
            status = "[green]READY[/green]" if success else "[red]BLOCKED[/red]"
            table.add_row(name, status, detail)
            if not success:
                all_passed = False
        
        console.print(table)
        
        if not all_passed:
            console.print("⚠️ [bold yellow]Pre-flight warnings detected. Some audit modules may exhibit drift.[/bold yellow]\n")
        else:
            console.print("✨ [bold green]Environment verified. Proceeding to Agent Audit.[/bold green]\n")
            
        return all_passed

def run_preflight(target_path=".", target_cloud="google"):
    engine = PreflightEngine(target_path)
    return engine.run_all(target_cloud)
