import os
import unittest


class TestPromptsImport(unittest.TestCase):
    def test_prompts_module_imports(self) -> None:
        import rcl_rlm.prompts  # noqa: F401

    def test_build_system_prompt(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        with_contacts = build_system_prompt(has_contacts=True)
        without_contacts = build_system_prompt(has_contacts=False)

        self.assertIn("messages_with_contacts", with_contacts)
        self.assertNotIn("messages_with_contacts", without_contacts)
        self.assertNotIn("Contacts Relationship (when available)", without_contacts)


class TestTipsProfiles(unittest.TestCase):
    def test_full_profile_includes_all_tips(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        prompt = build_system_prompt(tips_profile="full")
        # Check for content from each tips section
        self.assertIn("CRITICAL: When to Use llm_query()", prompt)
        self.assertIn("CRITICAL: Batch vs Loop", prompt)
        self.assertIn("Strategy: PRE-FILTER → REFINE", prompt)
        self.assertIn("Cross-Source Synthesis", prompt)
        self.assertIn("Pre-filter Keywords", prompt)
        self.assertIn("Iterative Refinement (answer dict)", prompt)

    def test_minimal_profile_includes_critical_only(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        prompt = build_system_prompt(tips_profile="minimal")
        # Critical tips should be present
        self.assertIn("CRITICAL: When to Use llm_query()", prompt)
        self.assertIn("CRITICAL: Batch vs Loop", prompt)
        # Other tips should be absent
        self.assertNotIn("Cross-Source Synthesis", prompt)
        self.assertNotIn("Pre-filter Keywords", prompt)
        self.assertNotIn("Iterative Refinement (answer dict)", prompt)

    def test_bench_profile_includes_critical_guidance(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        prompt = build_system_prompt(tips_profile="bench")
        # Critical guidance tips should be present (needed for benchmark accuracy)
        self.assertIn("CRITICAL: When to Use llm_query()", prompt)
        self.assertIn("Strategy: PRE-FILTER → REFINE", prompt)
        # Benchmark hygiene should be present
        self.assertIn("Benchmark Output Hygiene", prompt)
        # But optional tips should be absent
        self.assertNotIn("Cross-Source Synthesis", prompt)
        self.assertNotIn("Pre-filter Keywords", prompt)
        self.assertNotIn("Iterative Refinement (answer dict)", prompt)
        # Schema and rules should still be present
        self.assertIn("## Schema", prompt)
        self.assertIn("## Rules", prompt)
        self.assertIn("## Functions", prompt)

    def test_none_profile_excludes_all_tips(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        prompt = build_system_prompt(tips_profile="none")
        # Same as bench - all tips should be absent
        self.assertNotIn("CRITICAL: When to Use llm_query()", prompt)
        self.assertNotIn("Pre-filter Keywords", prompt)

    def test_env_variable_override(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        # Set environment variable
        old_value = os.environ.get("RECALL_RLM_TIPS_PROFILE")
        try:
            os.environ["RECALL_RLM_TIPS_PROFILE"] = "minimal"
            prompt = build_system_prompt()  # No explicit tips_profile
            # Should use minimal profile from env
            self.assertIn("CRITICAL: When to Use llm_query()", prompt)
            self.assertNotIn("Cross-Source Synthesis", prompt)
        finally:
            if old_value is not None:
                os.environ["RECALL_RLM_TIPS_PROFILE"] = old_value
            else:
                os.environ.pop("RECALL_RLM_TIPS_PROFILE", None)

    def test_explicit_profile_overrides_env(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        # Set environment variable to minimal
        old_value = os.environ.get("RECALL_RLM_TIPS_PROFILE")
        try:
            os.environ["RECALL_RLM_TIPS_PROFILE"] = "minimal"
            # But explicitly request full
            prompt = build_system_prompt(tips_profile="full")
            # Should use full profile, not minimal from env
            self.assertIn("Cross-Source Synthesis", prompt)
            self.assertIn("Pre-filter Keywords", prompt)
            self.assertIn("Iterative Refinement (answer dict)", prompt)
        finally:
            if old_value is not None:
                os.environ["RECALL_RLM_TIPS_PROFILE"] = old_value
            else:
                os.environ.pop("RECALL_RLM_TIPS_PROFILE", None)

    def test_invalid_profile_falls_back_to_full(self) -> None:
        from rcl_rlm.prompts import build_system_prompt

        prompt = build_system_prompt(tips_profile="invalid_profile")
        # Should fall back to full profile
        self.assertIn("CRITICAL: When to Use llm_query()", prompt)
        self.assertIn("Cross-Source Synthesis", prompt)
        self.assertIn("Pre-filter Keywords", prompt)


if __name__ == "__main__":
    unittest.main()
