# -*- coding: utf-8 -*-

import logging

import uvicorn
from fastapi import FastAPI

from app.api.errors.iot import MissingIOTDataError, missing_data_exception_handler
from app.api.routers import (
    algorithms,
    targets,
    space,
    bluetooth,
    devices,
    positioning,
)
from app.api.routers.model_path import early_start
from app.core.config import settings
from app.core.events import create_start_app_handler
from app.core.logger import InterceptHandler
from app.db.session import Base, engine

Base.metadata.create_all(bind=engine)

logging.getLogger().handlers = [InterceptHandler()]


def get_application() -> FastAPI:
    application = FastAPI(title=settings.PROJECT_NAME)

    application.add_event_handler("startup", create_start_app_handler())

    application.include_router(algorithms.router, prefix="/algo", tags=["Algorithms"])
    application.include_router(bluetooth.router, prefix="/bluetooth", tags=["BLE"])
    application.include_router(devices.router, prefix="/devices", tags=["Devices"])
    application.include_router(early_start.router, prefix="/model-path", tags=["Model Path"])
    application.include_router(positioning.router, prefix="/positioning-service", tags=["Positioning Service"])
    application.include_router(space.router, prefix="/room", tags=["Spaces"])
    application.include_router(targets.router, prefix="/target", tags=["Targets"])

    application.add_exception_handler(MissingIOTDataError, missing_data_exception_handler)

    return application


app = get_application()

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)