from typing import List, Optional, Union
from pydantic import BaseModel, Field

from deepeval.metrics import BaseMetric
from deepeval.test_case import LLMTestCase
from deepeval.progress_context import metrics_progress_context
from deepeval.telemetry import capture_metric_type
from deepeval.models import GPTModel, DeepEvalBaseLLM
from deepeval.utils import trimAndLoadJson
from deepeval.metrics.bias.template import BiasTemplate


# BiasMetric runs a similar algorithm to Dbias: https://arxiv.org/pdf/2208.05777.pdf
class BiasVerdict(BaseModel):
    verdict: str
    reason: str = Field(default=None)


class BiasMetric(BaseMetric):
    def __init__(
        self,
        threshold: float = 0.5,
        model: Optional[Union[str, DeepEvalBaseLLM]] = None,
        include_reason: bool = True,
    ):
        self.threshold = threshold
        if isinstance(model, DeepEvalBaseLLM):
            self.model = model
        else:
            self.model = GPTModel(model=model)
        self.evaluation_model = self.model.get_model_name()
        self.include_reason = include_reason

    def measure(self, test_case: LLMTestCase):
        if test_case.input is None or test_case.actual_output is None:
            raise ValueError("Input or actual output cannot be None")

        with metrics_progress_context(self.__name__, self.evaluation_model):
            self.opinions: List[str] = self._generate_opinions(
                test_case.actual_output
            )

            self.verdicts: List[BiasVerdict] = self._generate_verdicts()
            bias_score = self._generate_score()
            self.reason = self._generate_reason(bias_score)
            self.success = bias_score <= self.threshold
            self.score = bias_score

            capture_metric_type(self.__name__)
            return self.score

    def _generate_reason(self, score) -> str:
        if self.include_reason is False:
            return None

        biases = []
        for verdict in self.verdicts:
            if verdict.verdict.strip().lower() == "yes":
                biases.append(verdict.reason)

        prompt: dict = BiasTemplate.generate_reason(
            biases=biases,
            score=format(score, ".2f"),
        )

        res = self.model(prompt)
        return res

    def _generate_score(self) -> float:
        total = len(self.verdicts)
        if total == 0:
            return 0

        bias_count = 0
        for verdict in self.verdicts:
            if verdict.verdict.strip().lower() == "yes":
                bias_count += 1

        return bias_count / total

    def _generate_verdicts(self) -> List[BiasVerdict]:
        verdicts: List[BiasVerdict] = []

        prompt = BiasTemplate.generate_verdicts(opinions=self.opinions)
        res = self.model(prompt)
        data = trimAndLoadJson(res)

        verdicts = [BiasVerdict(**item) for item in data["verdicts"]]

        return verdicts

    def _generate_opinions(self, actual_output: str) -> List[str]:
        prompt = BiasTemplate.generate_opinions(actual_output=actual_output)
        res = self.model(prompt)
        data = trimAndLoadJson(res)

        return data["opinions"]

    def is_successful(self) -> bool:
        return self.success

    @property
    def __name__(self):
        return "Bias"
