"""
Instagram Scraper - Logging utilities
Professional logging with timestamps and levels
"""

import logging
import sys
from pathlib import Path
from typing import Optional

from .config import ScraperConfig


def setup_logger(
    name: str,
    log_file: Optional[str] = None,
    level: str = 'INFO',
    log_to_console: bool = True,
    config: Optional[ScraperConfig] = None
) -> logging.Logger:
    """
    Setup professional logger with file and console handlers

    Args:
        name: Logger name
        log_file: Path to log file (optional)
        level: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
        log_to_console: Whether to log to console
        config: ScraperConfig instance (optional)

    Returns:
        Configured logger instance
    """
    if config is None:
        config = ScraperConfig()

    logger = logging.getLogger(name)
    logger.setLevel(getattr(logging, level.upper()))

    # Clear existing handlers
    logger.handlers.clear()

    # Format: 2025-01-21 10:30:45 [INFO] scraper_name: Message
    formatter = logging.Formatter(
        config.log_format,
        datefmt=config.log_date_format
    )

    # Console handler
    if log_to_console:
        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setFormatter(formatter)
        logger.addHandler(console_handler)

    # File handler
    if log_file:
        log_path = Path(log_file)
        log_path.parent.mkdir(parents=True, exist_ok=True)
        file_handler = logging.FileHandler(log_file, encoding='utf-8')
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

    return logger
