from typing import List, Optional
from murnitur.annotation.evaluation.dataset.api import Golden, ConversationalGolden
from murnitur.annotation.evaluation.test_case import LLMTestCase, ConversationalTestCase


def convert_test_cases_to_goldens(
    test_cases: List[LLMTestCase],
) -> List[Golden]:
    goldens = []
    for test_case in test_cases:
        golden = {
            "input": test_case.input,
            "actual_output": test_case.actual_output,
            "expected_output": test_case.expected_output,
            "context": test_case.context,
            "retrieval_context": test_case.retrieval_context,
        }
        goldens.append(Golden(**golden))
    return goldens


def convert_goldens_to_test_cases(
    goldens: List[Golden],
    _alias: Optional[str] = None,
    _id: Optional[str] = None,
) -> List[LLMTestCase]:
    test_cases = []
    for index, golden in enumerate(goldens):
        test_case = LLMTestCase(
            input=golden.input,
            actual_output=golden.actual_output,
            expected_output=golden.expected_output,
            context=golden.context,
            retrieval_context=golden.retrieval_context,
            additional_metadata=golden.additional_metadata,
            comments=golden.comments,
            _dataset_alias=_alias,
            _dataset_id=_id,
            _dataset_rank=index,
        )
        test_cases.append(test_case)
    return test_cases


def convert_convo_goldens_to_convo_test_cases(
    convo_goldens: List[ConversationalGolden],
    _alias: Optional[str] = None,
    _id: Optional[str] = None,
) -> List[ConversationalTestCase]:
    conv_test_cases = []
    for index, convo_golden in enumerate(convo_goldens):
        conv_test_case = ConversationalTestCase(
            additional_metadata=convo_golden.additional_metadata,
            comments=convo_golden.comments,
            messages=convert_goldens_to_test_cases(convo_golden.messages),
            _dataset_alias=_alias,
            _dataset_id=_id,
            _dataset_rank=index,
        )
        conv_test_cases.append(conv_test_case)
    return conv_test_cases
