import json


class Benchmark_results:
    def __init__(self, results_file=None):
        # Allow results file to be set via argument or use default
        self.results_file = results_file or "benchmark_results.json"
        self.results = self._load_results()

    def add_result(
        self,
        model,
        hardware,
        accuracy_focused,
        accuracy_source,
        avg_time_per_question,
        context_window,
        temperature,
        ldr_version,
        date_tested,
        notes="",
    ):
        # add a new benchmark result, as noted by issue
        result = {
            "model": model,
            "hardware": hardware,
            "accuracy_focused": accuracy_focused,
            "accuracy_source": accuracy_source,
            "avg_time_per_question": avg_time_per_question,
            "context_window": context_window,
            "temperature": temperature,
            "ldr_version": ldr_version,
            "date_tested": date_tested,
            "notes": notes,
        }

        self.results.append(result)
        self._save_results()
        return True

    def get_all(self):
        # Getting all benchmark results
        return self.results

    def get_best(self, sort_by="accuracy_focused"):
        """get best performing models"""
        # Validate that sort_by is a valid key in the result dictionaries
        if self.results:
            if sort_by not in self.results[0]:
                raise ValueError(
                    f"Invalid sort_by key: '{sort_by}'. Valid keys are: {list(self.results[0].keys())}"
                )
        if sort_by == "avg_time_per_question":
            return sorted(
                self.results, key=lambda x: x["avg_time_per_question"]
            )
        return sorted(self.results, key=lambda x: x[sort_by], reverse=True)

    def _load_results(self):
        # Load results from file

        try:
            with open(self.results_file, "r", encoding="utf-8") as f:
                return json.load(f)
        except FileNotFoundError:
            return []

    def _save_results(self):
        # Save results to file.
        from ...security.file_write_verifier import write_json_verified

        write_json_verified(
            self.results_file,
            self.results,
            "benchmark.allow_file_output",
            context="benchmark results",
        )
