"""Tests for screening system message generation."""

import pytest

from crystallise.screening.sysmsg import (
    get_opposite_decision_type,
    make_cluster_selection_system_message,
    make_clustering_system_message,
    make_exclusion_generation_prompt,
    make_labelling_system_message,
    make_rating_reasoning_system_message,
)


class TestMakeLabellingSystemMessage:
    def test_returns_string_with_questions_and_criteria(self):
        questions = ["What treatments are effective?"]
        inc_exc = {"Population": {"include": ["Adults"], "exclude": ["Children"]}}
        result = make_labelling_system_message(questions, inc_exc)
        assert isinstance(result, str)
        assert "What treatments are effective?" in result
        assert "Adults" in result
        assert "Children" in result
        assert "1 - Definitely exclude" in result
        assert "5 - Definitely include" in result

    def test_empty_inputs(self):
        result = make_labelling_system_message([], {})
        assert isinstance(result, str)
        assert "No research questions provided" in result


class TestMakeRatingReasoningSystemMessage:
    def test_returns_string_with_score_scale(self):
        questions = ["Does drug X work?"]
        inc_exc = {"Outcome": {"include": ["Mortality"]}}
        result = make_rating_reasoning_system_message(questions, inc_exc)
        assert isinstance(result, str)
        assert "1 - Definitely exclude" in result
        assert "5 - Definitely include" in result
        assert "Does drug X work?" in result
        assert "50 words" in result


class TestMakeClusteringSystemMessage:
    def test_returns_string_with_decision_type(self):
        criteria_names = ["Population", "Intervention"]
        result = make_clustering_system_message(criteria_names, "excluded", 10)
        assert isinstance(result, str)
        assert "excluded" in result
        assert "Population" in result
        assert "Intervention" in result
        assert "10" in result

    def test_appends_d_to_decision_type(self):
        result = make_clustering_system_message(["Population"], "exclude", 5)
        assert "excluded" in result

    def test_included_decision_type(self):
        result = make_clustering_system_message(["Population"], "included", 5)
        assert "included" in result
        assert "in the literature review" in result


class TestMakeClusterSelectionSystemMessage:
    def test_returns_string_with_cluster_info(self):
        clusters = [
            {"cluster_name": "Treatment", "cluster_description": "Treatment studies"},
            {"cluster_name": "Safety", "cluster_description": "Safety studies"},
        ]
        result = make_cluster_selection_system_message(clusters, "included")
        assert isinstance(result, str)
        assert "Treatment" in result
        assert "Safety" in result
        assert "included" in result

    def test_excluded_decision_type(self):
        clusters = [{"cluster_name": "Reviews", "cluster_description": "Review papers"}]
        result = make_cluster_selection_system_message(clusters, "excluded")
        assert "excluded" in result
        assert "excluded from" in result


class TestGetOppositeDecisionType:
    def test_included_returns_excluded(self):
        assert get_opposite_decision_type("included") == "excluded"

    def test_excluded_returns_included(self):
        assert get_opposite_decision_type("excluded") == "included"

    def test_invalid_raises(self):
        with pytest.raises(ValueError):
            get_opposite_decision_type("maybe")


class TestMakeExclusionGenerationPrompt:
    def test_returns_string_with_project_context(self):
        result = make_exclusion_generation_prompt(
            "A systematic review of cancer treatments",
            ["What is the efficacy of drug X?"],
        )
        assert isinstance(result, str)
        assert "cancer treatments" in result
        assert "drug X" in result
        assert "PICO" in result
        assert "exclusion criteria" in result.lower()

    def test_empty_questions(self):
        result = make_exclusion_generation_prompt("Project description", [])
        assert isinstance(result, str)
        assert "No research questions provided" in result
