import sys
import os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import pandas as pd
import matplotlib.pyplot as plt

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
RESULTS_PATH = os.path.join(PROJECT_ROOT, "experiments", "results.csv")
PLOTS_DIR = os.path.join(PROJECT_ROOT, "experiments", "plots")

try:
    data = pd.read_csv(RESULTS_PATH)
except FileNotFoundError:
    print(f"Run benchmark.py first to generate {RESULTS_PATH}")
    exit(1)

os.makedirs(PLOTS_DIR, exist_ok=True)

for maze_name in data["maze"].unique():
    maze_data = data[data["maze"] == maze_name]
    plt.figure(figsize=(8, 5))
    plt.bar(maze_data["strategy"], maze_data["time_ms"], color=['blue', 'green', 'red'])
    plt.title(f"{maze_name} maze - Performance Comparison", fontsize=14)
    plt.ylabel("Time (ms)", fontsize=12)
    plt.xlabel("Strategy", fontsize=12)
    plt.grid(axis='y', alpha=0.3)
    plt.tight_layout()

    plot_filename = os.path.join(PLOTS_DIR, f"{maze_name.replace('.txt', '')}.png")
    plt.savefig(plot_filename, dpi=150)
    plt.close()
    print(f"Saved plot for {maze_name}")

print(f"All plots saved to {PLOTS_DIR}")