from typing import Any

from litestar import Litestar, get
from litestar.contrib.repository.exceptions import (
    RepositoryError as RepositoryException,
)
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyPlugin
from litestar.openapi import OpenAPIConfig

from app.controllers import create_router
from app.database import db_config, provide_transaction
from app.lib import exceptions
from app.lib.service import ServiceError

def create_app(**kwargs: Any) -> Litestar:
    return Litestar(
        route_handlers=[create_router()],
        openapi_config=OpenAPIConfig(title="My API", version="1.0.0"),
        dependencies={"session": provide_transaction},
        plugins=[SQLAlchemyPlugin(db_config)],
        exception_handlers={
            RepositoryException: exceptions.repository_exception_to_http_response,  # type: ignore[dict-item]
            ServiceError: exceptions.service_exception_to_http_response,  # type: ignore[dict-item]
        },
        debug=True,
        **kwargs,
    )


app = create_app()
