import time
from typing import NamedTuple

from .maze import Maze
from .strategies import PathFindingStrategy


class SearchStats(NamedTuple):
    time_ms: float
    visited_cells: int
    path_length: int


class MazeSolver:
    def __init__(self, maze: Maze):
        self.maze = maze
        self.strategy = None

    def set_strategy(self, strategy: PathFindingStrategy):
        self.strategy = strategy

    def solve(self) -> SearchStats:
        if self.strategy is None:
            raise ValueError("Strategy not set")

        start_time = time.perf_counter()
        path = self.strategy.find_path(self.maze, self.maze.start, self.maze.exit)
        end_time = time.perf_counter()
        time_ms = (end_time - start_time) * 1000

        return SearchStats(
            time_ms=time_ms,
            visited_cells=self.strategy.visited_count,
            path_length=len(path),
        )
