import logging
import asyncio
from typing import Optional

from core.voice import VoiceClient
from .identity import Identity
from .context import ContextManager

logger = logging.getLogger("core.conductor.engine")

class Conductor:
    """
    The Orchestrator.
    Manages Identity, Context, and the Voice/Vision Bridge.
    """
    def __init__(self):
        self.identity = Identity()
        self.context = ContextManager()
        self.voice_client = VoiceClient()
        self.is_alive = False
        
    async def boot(self):
        """
        System Boot Sequence.
        """
        logger.info("Conductor: BOOT SEQUENCE INITIATED")
        
        # 1. Load Identity
        if not self.identity.load():
            raise RuntimeError("Conductor failed to load Identity.")
            
        # 2. Inject System Instruction into Voice Client
        # (Assuming VoiceClient wraps LlmAgent which takes system_instruction)
        # We might need to subclass or configure VoiceClient to accept this.
        # For now, we log intent.
        sys_prompt = self.identity.get_system_instruction()
        logger.info("Conductor: Identity Injected into Kernel")
        
        self.is_alive = True
        logger.info("Conductor: ONLINE")
        
    async def run(self):
        """
        Main Event Loop.
        """
        if not self.is_alive:
            await self.boot()
            
        # Start Voice/Vision Bridge
        await self.voice_client.start()
        
    async def shutdown(self):
        logger.info("Conductor: SHUTTING DOWN")
        await self.voice_client.stop()
        self.is_alive = False
