"""
Genesis V2 Identity Manager
============================
Loads agent persona and system instructions.
"""

import logging
from pathlib import Path
from typing import Optional

logger = logging.getLogger("genesis_v2.identity")


class Identity:
    """Manages the agent's identity and system instruction."""
    
    def __init__(self):
        self.name: str = "Genesis"
        self.persona: str = ""
        self.system_instruction: str = ""
        self._loaded = False
    
    def load(self, rules_dir: Optional[Path] = None):
        """Load identity from rules directory."""
        if rules_dir is None:
            rules_dir = Path(__file__).parent.parent.parent / ".agent" / "rules"
        
        # Try to load project-goals.md as the primary identity source
        goals_file = rules_dir / "project-goals.md"
        if goals_file.exists():
            self.persona = goals_file.read_text(encoding="utf-8")
            self.name = "Antigravity"
        else:
            self.persona = "You are Genesis, an AI agent that helps with coding and system management."
            self.name = "Genesis"
        
        self._loaded = True
        logger.info(f"Identity Loaded: {self.name}")
    
    def get_system_instruction(self) -> str:
        """Get the full system instruction for the agent."""
        if not self._loaded:
            self.load()
        return self.persona
