#!/usr/bin/env python3
"""
{{ agent_name }} Agent Main Entry Point
Generated by ABI-Core scaffolding
"""

{% if with_web_interface %}
import threading
import uvicorn
{% endif %}

from {{ agent_file_name }} import {{ agent_class_name }}
{% if with_web_interface %}
from web_interface import AgentWebInterface
{% endif %}
from abi_core.common.a2a_server import start_server
from abi_core.common.utils import abi_logging

# Import configuration and agent card
from config import config, AGENT_CARD

def {{ agent_name }}_factory() -> {{ agent_class_name }}:
    """
    Factory function to create {{ agent_name }} instance
    
    Returns:
        {{ agent_class_name }}: Configured agent instance
    """
    try:
        abi_logging(f'[✅] Agent card loaded: {AGENT_CARD.name}')
        abi_logging(f'[📋] Description: {AGENT_CARD.description}')
        abi_logging(f'[🔧] Capabilities: {", ".join(AGENT_CARD.skills[1].tags) if hasattr(AGENT_CARD, "skills") else "No skills dumb agent"}')
        
        # Create and return new agent instance
        agent_instance = {{ agent_class_name }}()
        abi_logging(f'[🚀] {{ agent_name }} agent initialized successfully')
        
        {% if with_web_interface %}
        # Start web interface in separate thread
        def start_web_server_interface():
            web_interface = AgentWebInterface(
                agent_instance, 
                interface_name="{{ agent_display_name }} Web Interface"
            )
            abi_logging(f'[🌐] Starting {{ agent_display_name }} Web Server at 0.0.0.0:{config.WEB_INTERFACE_PORT}')
            uvicorn.run(
                web_interface.app, 
                host="0.0.0.0", 
                port=config.WEB_INTERFACE_PORT,
                log_level="info"
            )

        web_thread = threading.Thread(target=start_web_server_interface, daemon=True)
        web_thread.start()
        abi_logging(f'[✅] {{ agent_display_name }} web interface started on port {config.WEB_INTERFACE_PORT}')
        {% endif %}
        
        return agent_instance
        
    except Exception as e:
        abi_logging(f'[❌] Error creating {{ agent_name }} agent: {e}')
        raise

def main():
    """Main entry point for {{ agent_name }} agent"""
    
    abi_logging(f'[🌟] Starting {{ agent_name }} Agent Server')
    abi_logging(f'[🌐] Host: 0.0.0.0')
    abi_logging(f'[🔌] Port: {config.AGENT_PORT}')
    abi_logging(f'[📄] Agent Card: {config.AGENT_CARD}')
    abi_logging(f'[🤖] Model: {config.MODEL_NAME}')
    abi_logging(f'[🔗] Ollama: {config.OLLAMA_HOST}')
    
    try:
        # Create agent
        agent = {{ agent_name }}_factory()
        
        # Start the A2A server with the agent card
        start_server(
            host='0.0.0.0',
            port=config.AGENT_PORT,
            agent_card=AGENT_CARD,
            agent=agent
        )
        
    except KeyboardInterrupt:
        abi_logging('[🛑] {{ agent_name }} agent stopped by user')
        return 0
    except Exception as e:
        abi_logging(f'[💥] Fatal error starting {{ agent_name }} agent: {e}')
        return 1

if __name__ == '__main__':
    exit(main())