import logging.config
from pathlib import Path

from setech.utils import get_logger

from service.config import settings


def init_logging() -> None:
    from service.config import LOGGING

    Path("logs").mkdir(parents=True, exist_ok=True)
    logging.config.dictConfig(LOGGING)


def initialize_sentry() -> None:
    if settings.sentry_url:
        try:
            import sentry_sdk  # type: ignore
            from sentry_sdk.integrations.fastapi import FastApiIntegration  # type: ignore
            from sentry_sdk.integrations.starlette import StarletteIntegration  # type: ignore

            sentry_sdk.init(
                dsn=str(settings.sentry_url),
                enable_tracing=True,
                environment=settings.environment,
                integrations=[
                    StarletteIntegration(transaction_style="endpoint"),
                    FastApiIntegration(transaction_style="endpoint"),
                ],
            )
        except (Exception, ModuleNotFoundError, ImportError) as e:  # noqa
            get_logger().exception("Unable to set up Sentry integration", exc_info=e)
