#!/usr/bin/env python3
"""
Logging configuration for Debian Software Manager.
"""

# logging_config.py
import logging
import os
from logging.handlers import TimedRotatingFileHandler
import gettext

_ = gettext.translation('deb-software', fallback=True).gettext

# Define the log directory and file path
SAVE_DIR = os.path.expanduser("~/.config/deb-software")
LOG_PATH = os.path.join(SAVE_DIR, "deb-software.log")

def setup_logging():
    """
    Configure application-wide logging.
    Disables PIL debug logging, sets up a rotating file handler,
    and configures the root logger.
    """
    os.makedirs(SAVE_DIR, exist_ok=True)

    # Disable PIL debug logging
    logging.getLogger("PIL").setLevel(logging.WARNING)

    # Create a timed rotating file handler
    handler = TimedRotatingFileHandler(
        LOG_PATH,
        when='midnight',
        interval=1,
        backupCount=10,
    )

    # Set the logging level and format
    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(levelname)s - %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    )
    handler.setFormatter(formatter)

    # Rotate logs immediately on startup
    handler.doRollover()

    # Configure the root logger
    logging_logger = logging.getLogger()
    logging_logger.addHandler(handler)
    logging_logger.setLevel(logging.DEBUG)

    return logging_logger

# Initialize logging when the module is imported
logger = setup_logging()
