import json
import os
import re
import subprocess
import sys
from pathlib import Path


class SevenDimensionalAudit:
    def __init__(self):
        self.results = {}

    def run(self):
        print("🏛️ [AFO Kingdom] Initializing 7-Dimensional Audit...")
        self.d1_truth()  # Functional Integrity
        self.d2_beauty()  # Structural Elegance
        self.d3_goodness()  # Logical Robustness
        self.d4_serenity()  # Operational Peace
        self.d5_eternity()  # Historical Continuity
        self.d6_piety()  # Safety & Security
        self.d7_wisdom()  # Efficiency & Performance
        self.report()

    def d1_truth(self):
        """眞 - Does it actually work? Checking pruned API endpoints."""
        print("  - [D1: Truth] Auditing API Health Post-Pruning...")
        fabric_path = "packages/afo-core/AFO/afo_agent_fabric.py"
        content = open(fabric_path).read()
        # Verify only one /ping exists and it's the refined one
        pings = re.findall(r'@router.get\("/ping"\)', content)
        self.results["truth"] = (
            "✅ Single Health Endpoint confirmed."
            if len(pings) == 1
            else "❌ Duplicate or missing endpoints."
        )

    def d2_beauty(self):
        """美 - Structural purity. Checking the refactored MCP Server."""
        print("  - [D2: Beauty] Quality Check of the New Command Center...")
        mcp_path = "packages/trinity-os/trinity_os/servers/afo_ultimate_mcp_server.py"
        try:
            # Try to use virtual environment's radon first, fallback to system radon
            venv_radon = os.path.join(os.getcwd(), ".venv", "bin", "radon")
            if os.path.exists(venv_radon):
                res = subprocess.check_output([venv_radon, "cc", mcp_path, "-s"]).decode()
            else:
                res = subprocess.check_output(["radon", "cc", mcp_path, "-s"]).decode()
            # Find the worst grade in the refactored file
            grades = re.findall(r" - ([A-F]) \(", res)
            if "F" not in grades and "E" not in grades:
                self.results["beauty"] = (
                    f"✅ Structural integrity maintained (Best Grade: {min(grades)})"
                )
            else:
                self.results["beauty"] = f"❌ Found low grade {max(grades)} in refactored code."
        except FileNotFoundError:
            self.results["beauty"] = "⚠️ Radon not installed. Install with: pip install radon"
        except subprocess.CalledProcessError as e:
            self.results["beauty"] = f"⚠️ Radon execution failed: {e}"
        except Exception as e:
            self.results["beauty"] = f"⚠️ Radon check failed: {e}"

    def d3_goodness(self):
        """善 - Logic robustness. Synthetic stress of the JSON parser."""
        print("  - [D3: Goodness] Stress testing edge cases...")
        sys.path.insert(0, "tools/dgm/upstream")
        try:
            from llm import extract_json_between_markers

            # Stress with deeply nested or weirdly formatted JSON
            weird_json = '```json\n{"data": [{"a": 1}, {"b": 2}], "status": "ok"}\n```'
            res = extract_json_between_markers(weird_json)
            if res and res.get("status") == "ok":
                self.results["goodness"] = "✅ Logic: Handles complex nested structures."
            else:
                self.results["goodness"] = "❌ Logic: Failed complex nested test."
        except:
            self.results["goodness"] = "⚠️ Import error in D3 logic check."

    def d4_serenity(self):
        """和/Serenity - Operational peace. Warning & Noise Check."""
        print("  - [D4: Serenity] Checking for runtime noise...")
        # Check for potential SyntaxWarning patterns that could cause runtime noise
        mcp_path = "packages/trinity-os/trinity_os/servers/afo_ultimate_mcp_server.py"
        benchmark_path = "tools/dgm/upstream/polyglot/benchmark.py"
        llm_path = "tools/dgm/upstream/llm.py"

        noise_files = [mcp_path, benchmark_path, llm_path]
        found_noise = False
        for f in noise_files:
            if os.path.exists(f):
                content = open(f).read()
                # Check for problematic escape sequences that could cause SyntaxWarnings
                # Look for backslash followed by backtick in string literals
                if "\\`" in content:
                    found_noise = True
                    break
        self.results["serenity"] = (
            "⚠️ Warnings: Found unescaped backticks causing noise."
            if found_noise
            else "✅ Serenity: Runtime noise-free."
        )

    def d5_eternity(self):
        """永 - History and Continuity."""
        print("  - [D5: Eternity] Blueprint audit...")
        # Verify the existence of implementation_plan in the brain
        brain_dir = Path("${HOME}/.gemini/antigravity/brain")
        # Find latest talk dir
        latest_dir = sorted(brain_dir.glob("*"))[-1]
        plans = list(latest_dir.glob("*.md"))
        self.results["eternity"] = (
            f"✅ {len(plans)} historical artifacts preserved in {latest_dir.name}"
        )

    def d6_piety(self):
        """孝/Piety - Safety & Security. Path Traversal Audit."""
        print("  - [D6: Safety] Security Audit of MCP Server...")
        mcp_path = "packages/trinity-os/trinity_os/servers/afo_ultimate_mcp_server.py"
        if os.path.exists(mcp_path):
            content = open(mcp_path).read()
            if "relative_to(workspace_path)" in content:
                self.results["safety"] = "✅ Piety: Path traversal protection is ACTIVE."
            else:
                self.results["safety"] = "❌ Piety: Path traversal protection is MISSING."

    def d7_wisdom(self):
        """慧/Wisdom - Resource Efficiency."""
        print("  - [D7: Wisdom] Dependency and Dead Code Check...")
        try:
            # Focus on core AFO modules only, exclude external dependencies
            afo_core_path = "packages/afo-core/AFO"
            if not os.path.exists(afo_core_path):
                self.results["wisdom"] = "⚠️ AFO core directory not found"
                return

            # Try to use virtual environment's vulture first, fallback to system vulture
            venv_vulture = os.path.join(os.getcwd(), ".venv", "bin", "vulture")
            if os.path.exists(venv_vulture):
                # Run vulture on core AFO modules only with minimal options
                res = subprocess.check_output(
                    [venv_vulture, afo_core_path, "--min-confidence", "100"],
                    stderr=subprocess.DEVNULL,
                ).decode()
            else:
                res = subprocess.check_output(
                    ["vulture", afo_core_path, "--min-confidence", "100"], stderr=subprocess.DEVNULL
                ).decode()

            count = len(res.splitlines())
            self.results["wisdom"] = f"✅ Wisdom: Core dead code reduced to {count} items."
        except FileNotFoundError:
            self.results["wisdom"] = "⚠️ Vulture not installed. Install with: pip install vulture"
        except subprocess.CalledProcessError:
            # Vulture found dead code or had issues - this is actually good for quality control
            self.results["wisdom"] = "✅ Wisdom: Dead code analysis completed (found unused code)"
        except Exception as e:
            self.results["wisdom"] = f"⚠️ Vulture check failed: {e}"

    def report(self):
        print("\n=== 📜 7-Dimensional Kingdom Audit Report ===")
        for dim, status in self.results.items():
            print(f"[{dim.upper().ljust(10)}] {status}")


if __name__ == "__main__":
    SevenDimensionalAudit().run()
