"""
Flask application factory for Code of Conquest.

Creates and configures the Flask application instance.
"""

import os
from flask import Flask
from flask_cors import CORS
from app.config import get_config
from app.utils.logging import setup_logging, get_logger


def create_app(environment: str = None) -> Flask:
    """
    Application factory pattern for creating Flask app.

    Args:
        environment: Environment name (development, production, etc.)
                    If None, uses FLASK_ENV from environment variables.

    Returns:
        Flask: Configured Flask application instance

    Example:
        >>> app = create_app('development')
        >>> app.run(debug=True)
    """
    # Get the path to the project root (parent of 'app' package)
    project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    # Create Flask app with correct template and static folders
    app = Flask(
        __name__,
        template_folder=os.path.join(project_root, 'templates'),
        static_folder=os.path.join(project_root, 'static')
    )

    # Load configuration
    config = get_config(environment)

    # Configure Flask from config object
    app.config['SECRET_KEY'] = config.secret_key
    app.config['DEBUG'] = config.app.debug

    # Set up logging
    setup_logging(
        log_level=config.logging.level,
        log_format=config.logging.format,
        log_file=config.logging.file_path if 'file' in config.logging.handlers else None
    )

    logger = get_logger(__name__)
    logger.info(
        "Starting Code of Conquest",
        version=config.app.version,
        environment=config.app.environment
    )

    # Configure CORS
    CORS(app, origins=config.cors.origins)

    # Store config in app context
    app.config['COC_CONFIG'] = config

    # Register error handlers
    register_error_handlers(app)

    # Register blueprints (when created)
    register_blueprints(app)

    logger.info("Application initialized successfully")

    return app


def register_error_handlers(app: Flask) -> None:
    """
    Register global error handlers for the application.

    Args:
        app: Flask application instance
    """
    from app.utils.response import (
        error_response,
        internal_error_response,
        not_found_response
    )

    logger = get_logger(__name__)

    @app.errorhandler(404)
    def handle_404(error):
        """Handle 404 Not Found errors."""
        logger.warning("404 Not Found", path=error.description)
        return not_found_response()

    @app.errorhandler(500)
    def handle_500(error):
        """Handle 500 Internal Server errors."""
        logger.error("500 Internal Server Error", error=str(error), exc_info=True)
        return internal_error_response()

    @app.errorhandler(Exception)
    def handle_exception(error):
        """Handle uncaught exceptions."""
        logger.error(
            "Uncaught exception",
            error=str(error),
            error_type=type(error).__name__,
            exc_info=True
        )
        return internal_error_response()


def register_blueprints(app: Flask) -> None:
    """
    Register Flask blueprints (API routes and web UI views).

    Args:
        app: Flask application instance
    """
    logger = get_logger(__name__)

    # ===== API Blueprints =====

    # Import and register health check API blueprint
    from app.api.health import health_bp
    app.register_blueprint(health_bp)
    logger.info("Health API blueprint registered")

    # Import and register auth API blueprint
    from app.api.auth import auth_bp
    app.register_blueprint(auth_bp)
    logger.info("Auth API blueprint registered")

    # Import and register characters API blueprint
    from app.api.characters import characters_bp
    app.register_blueprint(characters_bp)
    logger.info("Characters API blueprint registered")

    # Import and register sessions API blueprint
    from app.api.sessions import sessions_bp
    app.register_blueprint(sessions_bp)
    logger.info("Sessions API blueprint registered")

    # Import and register jobs API blueprint
    from app.api.jobs import jobs_bp
    app.register_blueprint(jobs_bp)
    logger.info("Jobs API blueprint registered")

    # Import and register game mechanics API blueprint
    from app.api.game_mechanics import game_mechanics_bp
    app.register_blueprint(game_mechanics_bp)
    logger.info("Game Mechanics API blueprint registered")

    # Import and register travel API blueprint
    from app.api.travel import travel_bp
    app.register_blueprint(travel_bp)
    logger.info("Travel API blueprint registered")

    # Import and register NPCs API blueprint
    from app.api.npcs import npcs_bp
    app.register_blueprint(npcs_bp)
    logger.info("NPCs API blueprint registered")

    # TODO: Register additional blueprints as they are created
    # from app.api import combat, marketplace, shop
    # app.register_blueprint(combat.bp, url_prefix='/api/v1/combat')
    # app.register_blueprint(marketplace.bp, url_prefix='/api/v1/marketplace')
    # app.register_blueprint(shop.bp, url_prefix='/api/v1/shop')
