"""
Visualization utilities for optimization results.

This module provides functions for generating visual representations
of benchmark and optimization results.
"""

from loguru import logger
from typing import List, Optional


# Check if matplotlib is available
try:
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure

    MATPLOTLIB_AVAILABLE = True
except ImportError:
    MATPLOTLIB_AVAILABLE = False
    logger.warning(
        "Matplotlib not available. Visualization functions will be limited."
    )


def plot_optimization_history(
    trial_values: List[float],
    best_values: List[float],
    output_file: Optional[str] = None,
    title: str = "Optimization History",
) -> Optional[Figure]:
    """
    Plot the optimization history.

    Args:
        trial_values: List of objective values for each trial
        best_values: List of best values observed up to each trial
        output_file: Path to save the plot (if None, returns figure without saving)
        title: Plot title

    Returns:
        Matplotlib figure or None if matplotlib is not available
    """
    if not MATPLOTLIB_AVAILABLE:
        logger.warning("Matplotlib not available. Cannot create plot.")
        return None

    fig, ax = plt.subplots(figsize=(10, 6))
    trials = list(range(1, len(trial_values) + 1))

    # Plot trial values and best values
    ax.plot(trials, trial_values, "o-", alpha=0.5, label="Trial Value")
    ax.plot(trials, best_values, "r-", label="Best Value")

    # Add labels and title
    ax.set_xlabel("Trial Number")
    ax.set_ylabel("Objective Value")
    ax.set_title(title)
    ax.grid(True, linestyle="--", alpha=0.7)
    ax.legend()

    # Save or return
    if output_file:
        fig.tight_layout()
        fig.savefig(output_file, dpi=300, bbox_inches="tight")
        logger.info(f"Saved optimization history plot to {output_file}")

    return fig
