import logging
from datetime import datetime, timezone
from logging import FileHandler, Formatter, getLogger
from pathlib import Path

from rich.logging import RichHandler


class LoggingMixin:
    def __init__(self):
        log_dir = Path("log")
        log_dir.mkdir(parents=True, exist_ok=True)
        log_path = log_dir / (datetime.now(tz=timezone.utc).strftime("%Y%m%d") + ".txt")

        file_handler = FileHandler(filename=str(log_path), encoding="utf-8")
        rich_handler = RichHandler(markup=True, rich_tracebacks=True)
        file_handler.setFormatter(Formatter("[%(asctime)s] %(levelname)s %(message)s"))
        rich_handler.setFormatter(Formatter("%(message)s"))

        self._logger = getLogger(__name__)
        self._logger.setLevel(logging.INFO)

        if not self._logger.handlers:
            self._logger.addHandler(file_handler)
            self._logger.addHandler(rich_handler)

    def log_info(self, message):
        self._logger.info(message)

    def log_error(self, message):
        self._logger.error(message)
