"""Tests para setup_logger."""

import logging
import os
import tempfile

from setup_logger import get_logger, setup_logger


def test_logger_tiene_dos_handlers():
    with tempfile.TemporaryDirectory() as log_dir:
        logger = setup_logger(name="test_two_handlers", log_dir=log_dir)
        assert len(logger.handlers) == 2
        # limpiar para no contaminar otros tests
        logger.handlers.clear()


def test_segundo_call_no_duplica_handlers():
    with tempfile.TemporaryDirectory() as log_dir:
        logger1 = setup_logger(name="test_idempotent", log_dir=log_dir)
        handler_count_after_first = len(logger1.handlers)
        logger2 = setup_logger(name="test_idempotent", log_dir=log_dir)
        assert logger1 is logger2
        assert len(logger2.handlers) == handler_count_after_first
        logger1.handlers.clear()


def test_archivo_se_crea_en_log_dir():
    with tempfile.TemporaryDirectory() as log_dir:
        logger = setup_logger(name="test_file_created", log_dir=log_dir)
        log_files = [f for f in os.listdir(log_dir) if f.endswith(".log")]
        assert len(log_files) == 1
        logger.handlers.clear()


def test_get_logger_retorna_logger_configurado():
    with tempfile.TemporaryDirectory() as log_dir:
        # Primero configurar para que get_logger encuentre handlers
        setup_logger(name="test_get_logger", log_dir=log_dir)
        logger = get_logger(name="test_get_logger")
        assert len(logger.handlers) == 2
        logger.handlers.clear()


def test_logger_level_es_debug():
    with tempfile.TemporaryDirectory() as log_dir:
        logger = setup_logger(name="test_level_debug", log_dir=log_dir, level=logging.DEBUG)
        assert logger.level == logging.DEBUG
        logger.handlers.clear()
