"""RichLogger Module
================

This module provides a multi-class design for logging messages and displaying a
console-based progress bar, with optional file logging, thread safety,
multi-line logging, color-coded bars, and more. Sphinx-style docstrings are
provided for each class and method.

---

Additional Enhancements:
1. Explicit Return Types
2. Error Handling in FileManager (basic example)
3. Potential Log Rotation (placeholder example)
4. Enhanced Progress Features (ETA)
5. Support for more windows or layouts (basic extension point)
6. Custom Logger Hierarchies (child loggers example)
7. Async / concurrency suggestions are partially addressed with thread lock
8. Testing with Mocks (stub test method)

"""

import time
from app.constants import LogLevel, TextColor
from app.core.rich_logger import RichLogger

########################################
# 4) Demo & Tests
########################################


def main_demo() -> None:
    """
    A small demo function showing usage of RichLogger.
    """
    logger = RichLogger(level=LogLevel.DEBUG, total_steps=10,
                        prefix="Loading", suffix="Done", show_eta=True)

    logger.debug("Program started.", color=TextColor.BRIGHT_CYAN)

    for i in range(10):
        time.sleep(0.1)
        logger.increment_progress()
        logger.debug(f"Step {i+1} of 10", color=TextColor.GREEN)

    logger.info("All steps completed.", color=TextColor.BRIGHT_YELLOW)

    time.sleep(1)
    logger.info("Exiting.")
    logger.close_file()


def test_no_logs() -> None:
    """
    Tests behavior when no logs are produced, only the progress bar.
    Expected behavior: The terminal should only show the progress bar
    from 0% to 100%. No other messages should be displayed.
    """
    logger = RichLogger(level=LogLevel.INFO, total_steps=5,
                        prefix="BarOnly", suffix="End")

    for _ in range(5):
        time.sleep(0.05)
        logger.increment_progress()

    logger.close_file()


def test_file_logging() -> None:
    """
    Demonstrates logging to a file as well.
    Question to user: Should we expect color-coded logs in the file or not?
    Currently, we write plain (uncolored) lines to the file.
    """
    logger = RichLogger(
        level=LogLevel.DEBUG,
        total_steps=3,
        prefix="FileTest",
        suffix="End",
        file_path="my_logs.txt",
        file_mode="a",
        max_file_size=500  # Rotate if file > 500 bytes
    )

    logger.info("Starting file log test", color=TextColor.CYAN)
    for i in range(3):
        time.sleep(0.05)
        logger.increment_progress()
        logger.debug(f"FileLog step {i+1} of 3", color=TextColor.GREEN)

    logger.info("File log test completed.", color=TextColor.BRIGHT_YELLOW)
    logger.close_file()


def test_multiline() -> None:
    """
    Tests the multiline logging. Should see multiple lines with the same level.
    """
    logger = RichLogger(level=LogLevel.DEBUG)
    text_block = """Line one\nLine two\nLine three\n"""
    logger.log_multiline(LogLevel.INFO, text_block, color=TextColor.MAGENTA)


def test_mocking() -> None:
    """
    Placeholder for a test that uses mocking to capture outputs.
    (We won't implement real mocking here, just a stub demonstration.)
    """
    # In an actual test, you'd mock sys.stdout or the file manager
    # to verify calls, e.g. with unittest.mock.


if __name__ == "__main__":
    main_demo()
    # test_no_logs()
    # test_file_logging()
    # test_multiline()
    # test_mocking()
