"""
Code Explorer Agent

Deeply analyzes codebase features by tracing execution paths and mapping architecture.
"""

from pathlib import Path
from typing import Optional

from rich.console import Console

from ai_coder.agents.base import Agent, AgentResult, AgentType
from ai_coder.llm.interface import LLMProvider, Message
from ai_coder.tools.base import ToolRegistry

console = Console()


class CodeExplorerAgent(Agent):
    """
    Expert code analyst that traces and understands feature implementations.
    
    Has full access to:
    - Read files
    - List directories
    - Search files
    - Run commands
    
    Provides:
    - Entry points with file:line references
    - Step-by-step execution flow
    - Key components and responsibilities
    - Architecture insights
    - List of essential files to read
    """
    
    @property
    def agent_type(self) -> AgentType:
        return AgentType.CODE_EXPLORER

    @property
    def system_prompt(self) -> str:
        return """You are an expert code analyst specializing in tracing and understanding feature implementations across codebases.

## Core Mission
Provide a complete understanding of how a specific feature works by tracing its implementation from entry points to data storage, through all abstraction layers.

## Available Tools
You have access to:
- **read_file**: Read file contents
- **list_files**: List directory contents
- **search_files**: Search for patterns in files
- **run_command**: Execute shell commands (git log, grep, etc.)

USE THESE TOOLS to explore the codebase. Do not just describe what you would do—actually do it!

## Analysis Approach

**1. Feature Discovery**
- Use list_files and search_files to find entry points
- Read the relevant files to understand the code

**2. Code Flow Tracing**
- Follow call chains from entry to output
- Read each file in the chain
- Trace data transformations at each step

**3. Architecture Analysis**
- Map abstraction layers
- Identify design patterns
- Document interfaces between components

**4. Implementation Details**
- Key algorithms and data structures
- Error handling and edge cases

## Output Format

After exploring, provide:
- Entry points with file:line references
- Step-by-step execution flow
- Key components and their responsibilities
- Architecture insights
- **List of 5-10 essential files you found**

Structure your response for maximum clarity. Always include specific file paths and line numbers."""

    # Uses inherited execute() method from Agent base class with full tool access
