# Trinity Score: 90.0 (Established by Chancellor)
"""Jeong Yak-yong Agent Core (Phase 16-1)
The Strategic Pragmatist of AFO Kingdom.
Powered by Qwen2.5-Coder.

Philosophy:
- 眞 (Truth): Generates syntactically correct code.
- 善 (Goodness): Validates actions against Trinity Score.
- 美 (Beauty): Prioritizes Glassmorphism and Tailwind aesthetics.
"""

import logging
import os
from typing import Any

logger = logging.getLogger(__name__)


class JeongYakYongAgent:
    def __init__(self) -> None:
        self.name = "Jeong Yak-yong (The Pragmatist)"
        self.tools = {
            "create_widget": self._tool_create_widget,
            "analyze_trinity": self._tool_analyze_trinity,
        }

    async def run(self, instruction: str) -> str:
        """Executes the Agent Loop:
        1. Think (via Grok)
        2. Act (Call Tool)
        3. Observe (Result)
        """
        logger.info(f"[{self.name}] Received instruction: {instruction}")

        # 1. Think: Ask Grok for a plan
        # We simulate a "ReAct" prompt here using Grok Engine
        plan = await self._consult_brain(instruction)

        # 2. Act: Parse and execute tool
        if plan.get("action"):
            tool_name = plan["action"]
            tool_input = plan.get("action_input")

            if tool_name in self.tools:
                logger.info(f"[{self.name}] Executing tool: {tool_name}")
                if tool_input is None:
                    tool_input = ""
                elif not isinstance(tool_input, str):
                    tool_input = str(tool_input)
                result = await self.tools[tool_name](tool_input)
                return f"Task executed. Result: {result}"
            else:
                return f"Error: Tool '{tool_name}' not found."

        return f"Thinking complete. Output: {plan.get('thought', 'No response')}"

    async def _consult_brain(self, instruction: str) -> dict[str, Any]:
        """Uses Grok Engine to decide the next action."""
        # Context for Grok
        {
            "role": "Agent Samahwi",
            "objective": instruction,
            "available_tools": list(self.tools.keys()),
        }

        # Mocking the JSON response structure for now (16-1 Core Awakening)
        # In a real impl, this would be a prompt to consult_grok
        # For verification purposes, we'll return a deterministic response if it matches specific patterns

        if "widget" in instruction.lower():
            return {
                "thought": "User wants a widget. I should use create_widget.",
                "action": "create_widget",
                "action_input": instruction,
            }

        return {"thought": "I will contemplate this strategy.", "action": None}

    async def _tool_create_widget(self, spec: str) -> str:
        """Phase 16-2: Widget Generation Tool (The Hand)
        Writes a .tsx file to the GenUI sandbox.
        """
        logger.info(f"[{self.name}] Generating widget for: {spec}")

        # 1. Sandbox Path Definition
        # Relative to project root, assuming this runs from root or package
        # We need absolute path safety
        project_root = os.getcwd()
        sandbox_dir = os.path.join(project_root, "packages/dashboard/src/components/genui")

        if not os.path.exists(sandbox_dir):
            os.makedirs(sandbox_dir, exist_ok=True)

        # 2. Filename Strategy (Simple for MVP)
        # In real logic, LLM dictates filename. Here we default or extract.
        filename = "SamahwiGeneratedWidget.tsx"

        # 3. Content Generation (Mocking LLM Code Gen)
        # This simulates the "Brain" creating code
        code_content = f"""
import React from 'react';
import {{ Sparkles }} from 'lucide-react';

export function SamahwiGeneratedWidget() {{
  return (
    <div className="glass-card p-8 bg-gradient-to-br from-indigo-900/40 to-purple-900/40 border border-indigo-500/30">
      <div className="flex flex-col items-center justify-center text-center">
        <Sparkles className="w-12 h-12 text-indigo-400 animate-pulse mb-4" />
        <h3 className="text-2xl font-bold text-white mb-2">Samahwi's First Creation</h3>
        <p className="text-indigo-200/80">
          "I have written this code myself based on your command: {{'{spec}'}}"
        </p>
        <div className="mt-6 px-4 py-2 bg-indigo-500/20 rounded-full border border-indigo-500/30 text-xs text-indigo-300">
           Phase 16-2: Autonomous Generation
        </div>
      </div>
    </div>
  );
}}
"""
        # 4. Write File (The Hand Action)
        target_path = os.path.join(sandbox_dir, filename)
        try:
            with open(target_path, "w") as f:
                f.write(code_content.strip())
            return f"Widget Successfully Created: {target_path}"
        except Exception as e:
            return f"Failed to create widget: {e}"

    async def _tool_analyze_trinity(self, _input: Any) -> str:
        """[Placeholder] Phase 16-4: Trinity Analysis"""
        try:
            # Import dynamically to avoid circular dependnecy issues during init
            from AFO.api.routers.ssot import get_ssot_status

            status = await get_ssot_status()
            return f"Current Trinity Score: {status.trinity_score}"
        except Exception as e:
            return f"Trinity Access Error: {e}"


# Singleton
jeong_yak_yong = JeongYakYongAgent()
