"""REST API for the CapInvest Platform."""

import logging
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from capinvest_core.api.app_loader import AppLoader
from capinvest_core.api.router.commands import router as router_commands
from capinvest_core.api.router.coverage import router as router_coverage
from capinvest_core.api.router.system import router as router_system
from capinvest_core.app.service.auth_service import AuthService
from capinvest_core.app.service.system_service import SystemService
from capinvest_core.env import Env

from capinvest_core.api.capspace_user_setting_handler import UserApiSettingMiddleware

logger = logging.getLogger("uvicorn.info")

system = SystemService().system_settings


@asynccontextmanager
async def lifespan(_: FastAPI):
    """Startup event."""
    yield


app = FastAPI(
    title=system.api_settings.title,
    description=system.api_settings.description,
    version=system.api_settings.version,
    terms_of_service=system.api_settings.terms_of_service,
    contact={
        "name": system.api_settings.contact_name,
        # "url": system.api_settings.contact_url,
        "email": system.api_settings.contact_email,
    },
    # license_info={
    #     "name": system.api_settings.license_name,
    #     "url": system.api_settings.license_url,
    # },
    servers=[
        {
            "url": s.url,
            "description": s.description,
        }
        for s in system.api_settings.servers
    ],
    lifespan=lifespan,
)
app.add_middleware(
    CORSMiddleware,
    allow_origins=system.api_settings.cors.allow_origins,
    allow_methods=system.api_settings.cors.allow_methods,
    allow_headers=system.api_settings.cors.allow_headers,
)
app.add_middleware(UserApiSettingMiddleware)
AppLoader.add_routers(
    app=app,
    routers=(
        [AuthService().router, router_system, router_coverage, router_commands]
        if Env().DEV_MODE
        else (
            [router_commands, router_coverage]
            if hasattr(router_commands, "routes") and router_commands.routes
            else [router_commands]
        )
    ),
    prefix=system.api_settings.prefix,
)
AppLoader.add_openapi_tags(app)
AppLoader.add_exception_handlers(app)

if __name__ == "__main__":
    # pylint: disable=import-outside-toplevel
    import uvicorn

    # This initializes the CapInvest environment variables so they can be read before uvicorn is run.
    Env()
    uvicorn_kwargs = system.python_settings.model_dump().get("uvicorn", {})
    uvicorn_reload = uvicorn_kwargs.pop("reload", None)

    if uvicorn_reload is None or uvicorn_reload:
        uvicorn_kwargs["reload"] = True

    uvicorn_app = uvicorn_kwargs.pop("app", "capinvest_core.api.rest_api:app")

    uvicorn.run(uvicorn_app, **uvicorn_kwargs)
