import csv
from maze import *
from maze_generator import *

def run_experiments():
    maze_configs = [
        ("small_random", lambda: random_maze(15, 15, wall_prob=0.3)),
        ("medium_recursive_div", lambda: recursive_division_maze(31, 31)),  #odd хз
        ("large_empty", lambda: empty_maze(100, 100)),
        ("large_random", lambda: random_maze(100, 100, wall_prob=0.25)),
        ("no_path", lambda: no_path_maze(20, 20)),
    ]

    algorithms = [("BFS", BFSPathFinding()),
                  ("DFS", DFSPathFinding()),
                  ("A*", AStarPathFinding())]

    results = []
    for name, gen_func in maze_configs:
        maze = gen_func()
        for alg_name, strategy in algorithms:
            solver = MazeSolver(maze, strategy)
            times, visited, lengths = [], [], []
            for _ in range(5):
                stats = solver.solve()
                times.append(stats.time_ms)
                visited.append(stats.visited)
                lengths.append(stats.path_length)
            avg_t = sum(times) / len(times)
            avg_v = sum(visited) / len(visited)
            avg_l = sum(lengths) / len(lengths)
            results.append([name, alg_name, avg_t, avg_v, avg_l])
            print(f"{name:20} {alg_name:5} time={avg_t:8.2f}ms visited={avg_v:8.1f} length={avg_l:5.1f}")

    with open("results_maze.csv", "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["maze", "algorithm", "time_ms", "visited", "path_length"])
        writer.writerows(results)
    print("saved results_maze.csv")

if __name__ == "__main__":
    import sys
    DEBUG = True
    if (len(sys.argv) > 1 and sys.argv[1] == "exp") or DEBUG:
        run_experiments()
    else:
        #run_interactive()
        pass