import logging
import logging.handlers
from pathlib import Path
from time import gmtime
from typing import Final

from lang_main.constants import (
    BASE_PATH,
    ENABLE_LOGGING,
    LOGGING_TO_FILE,
    LOGGING_TO_STDERR,
)
from lang_main.types import LoggingLevels

# ** config
logging.Formatter.converter = gmtime
LOG_FMT: Final[str] = '%(asctime)s | lang_main:%(module)s:%(levelname)s | %(message)s'
LOG_DATE_FMT: Final[str] = '%Y-%m-%d %H:%M:%S +0000'
LOG_FILE_FOLDER: Final[Path] = BASE_PATH / 'logs'
if not LOG_FILE_FOLDER.exists():
    LOG_FILE_FOLDER.mkdir(parents=True)

LOG_FILE_PATH: Final[Path] = LOG_FILE_FOLDER / 'lang-main.log'

# ** formatters
logger_all_formater = logging.Formatter(fmt=LOG_FMT, datefmt=LOG_DATE_FMT)

# ** handlers
null_handler = logging.NullHandler()
if ENABLE_LOGGING and LOGGING_TO_STDERR:
    logger_all_handler_stderr = logging.StreamHandler()
    logger_all_handler_stderr.setLevel(LoggingLevels.WARNING)
    logger_all_handler_stderr.setFormatter(logger_all_formater)
else:
    logger_all_handler_stderr = null_handler

if ENABLE_LOGGING and LOGGING_TO_FILE:
    logger_all_handler_file = logging.handlers.RotatingFileHandler(
        LOG_FILE_PATH,
        encoding='utf-8',
        maxBytes=5_242_880,
        backupCount=1,
    )
    logger_all_handler_file.setLevel(LoggingLevels.DEBUG)
    logger_all_handler_file.setFormatter(logger_all_formater)
else:
    logger_all_handler_file = null_handler


# ** logging levels
LOGGING_LEVEL_ALL: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_PREPROCESS: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_PIPELINES: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_GRAPHS: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_TIMELINE: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_TOKEN_ANALYSIS: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_SHARED_HELPERS: Final[LoggingLevels] = LoggingLevels.DEBUG
LOGGING_LEVEL_RENDERING: Final[LoggingLevels] = LoggingLevels.DEBUG

# ** loggers and configuration
logger_all = logging.getLogger('lang_main')
logger_all.addHandler(logger_all_handler_stderr)
logger_all.addHandler(logger_all_handler_file)

logger_shared_helpers = logging.getLogger('lang_main.shared')
logger_shared_helpers.setLevel(LOGGING_LEVEL_SHARED_HELPERS)
logger_preprocess = logging.getLogger('lang_main.analysis.preprocessing')
logger_graphs = logging.getLogger('lang_main.analysis.graphs')
logger_graphs.setLevel(LOGGING_LEVEL_GRAPHS)
logger_timeline = logging.getLogger('lang_main.analysis.timeline')
logger_timeline.setLevel(LOGGING_LEVEL_TIMELINE)
logger_token_analysis = logging.getLogger('lang_main.analysis.tokens')
logger_token_analysis.setLevel(LOGGING_LEVEL_TOKEN_ANALYSIS)
logger_preprocess.setLevel(LOGGING_LEVEL_PREPROCESS)
logger_pipelines = logging.getLogger('lang_main.pipelines')
logger_pipelines.setLevel(LOGGING_LEVEL_PIPELINES)
logger_rendering = logging.getLogger('lang_main.render')
logger_rendering.setLevel(LOGGING_LEVEL_RENDERING)
