import asyncio
import os
from pathlib import Path

import inject
from litestar import (
    Litestar,
)
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.static_files import create_static_files_router
from litestar.template.config import TemplateConfig

from server.config import get_app_config
from server.infra.db import AsyncDB
from server.infra.web import BreedsController, DescriptionController, SeoController
from server.modules.descriptions import CharactersService, PGCharactersRepository
from server.modules.recognizer import RecognizerRepository, RecognizerService

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"


loop = asyncio.new_event_loop()


def inject_config(binder: inject.Binder):
    """initialization inject_config for server FastApi"""

    cnf = get_app_config()
    db = AsyncDB(cnf)
    loop.run_until_complete(db.connect())
    binder.bind(RecognizerService, RecognizerService(RecognizerRepository()))
    binder.bind(CharactersService, CharactersService(PGCharactersRepository(db)))


inject.configure(inject_config)
app = Litestar(
    debug=True,
    route_handlers=[
        BreedsController,
        DescriptionController,
        SeoController,
        create_static_files_router(path="/static", directories=["server/static"]),
    ],
    template_config=TemplateConfig(
        directory=Path("server/templates"),
        engine=JinjaTemplateEngine,
    ),
)
