import asyncio
import argparse
import logging
import sys

from app.agent_framework.agents.citation_detector import CitationDetectorAgent
from app.agent_framework.agents.content_generator_agent import ContentGeneratorAgent
from app.agent_framework.agents.deai_agent import DeAIAgent
from app.agent_framework.agents.geo_optimizer_agent import GEOOptimizerAgent
from app.agent_framework.agents.monitor_agent import MonitorAgent
from app.agent_framework.agents.schema_advisor import SchemaAdvisorAgent
from app.agent_framework.agents.competitor_analyzer import CompetitorAnalyzerAgent
from app.agent_framework.agents.trend_agent import TrendAgent

AGENTS = {
    "citation_detector": CitationDetectorAgent,
    "content_generator": ContentGeneratorAgent,
    "deai_agent": DeAIAgent,
    "geo_optimizer": GEOOptimizerAgent,
    "monitor": MonitorAgent,
    "schema_advisor": SchemaAdvisorAgent,
    "competitor_analyzer": CompetitorAnalyzerAgent,
    "trend_agent": TrendAgent,
    "all": None,
}


async def run_agent(name: str):
    if name == "all":
        agents = [cls() for cls in AGENTS.values() if cls is not None]
    else:
        cls = AGENTS.get(name)
        if cls is None:
            print(f"Unknown agent: {name}")
            sys.exit(1)
        agents = [cls()]

    for agent in agents:
        await agent.start()

    print(f"Agent(s) running: {[a.name for a in agents]}")
    try:
        await asyncio.Future()
    except asyncio.CancelledError:
        pass
    finally:
        for agent in agents:
            await agent.stop()


def main():
    parser = argparse.ArgumentParser(description="Run GEO Agent(s)")
    parser.add_argument("agent", choices=list(AGENTS.keys()), help="Agent name or 'all'")
    parser.add_argument("--log-level", default="INFO", help="Logging level")
    args = parser.parse_args()

    logging.basicConfig(level=getattr(logging, args.log_level.upper()))
    asyncio.run(run_agent(args.agent))


if __name__ == "__main__":
    main()
