"""Tests that business rules and safety guidelines are present in MCP instructions.

These rules are critical for customer-facing usage — if any are missing,
the LLM may give misleading advice or perform dangerous operations.
"""

import pytest

from influ2_mcp.server import mcp


@pytest.fixture
def instructions():
    return mcp.instructions or ""


class TestMetricsRules:
    """Verify all metrics analysis rules are in server instructions."""

    def test_statistical_significance_rule(self, instructions):
        """Creatives with < 200 impressions should be flagged."""
        assert "200 impressions" in instructions
        assert "insufficient data" in instructions.lower() or "statistic" in instructions.lower()

    def test_ctr_benchmarks_present(self, instructions):
        """CTR thresholds must be documented."""
        assert "0.15%" in instructions
        assert "0.20%" in instructions
        assert "Underperforming" in instructions or "underperforming" in instructions

    def test_fresh_creative_spike_warning(self, instructions):
        """New creatives spike — must warn about this."""
        assert "spike" in instructions.lower()
        assert "2 weeks" in instructions or "two weeks" in instructions.lower()

    def test_ad_fatigue_detection(self, instructions):
        """High impressions + low CTR = ad fatigue."""
        assert "fatigue" in instructions.lower()
        assert "0.12%" in instructions

    def test_platform_context_rule(self, instructions):
        """Must explain PBA CTR is not comparable to LinkedIn/Meta."""
        assert "LinkedIn" in instructions or "linkedin" in instructions
        assert "person-based" in instructions.lower()
        assert "misleading" in instructions.lower() or "not comparable" in instructions.lower()

    def test_impression_parity_explanation(self, instructions):
        """Uneven impressions between creatives should be explained."""
        assert "parity" in instructions.lower() or "evenly" in instructions.lower()

    def test_content_reuse_warning(self, instructions):
        """Reusing creatives across journey stages should be flagged."""
        assert "reuse" in instructions.lower() or "reusing" in instructions.lower()
        assert "journey stage" in instructions.lower() or "journey stages" in instructions.lower()


class TestSafetyRules:
    """Verify safety rules protect users from accidental data loss."""

    def test_replace_is_destructive_warning(self, instructions):
        """action='replace' must have a clear warning."""
        assert "replace" in instructions.lower()
        assert "REMOVE ALL" in instructions or "remove all" in instructions.lower()

    def test_delete_confirmation_required(self, instructions):
        """Deleting targets must require confirmation."""
        assert "delete" in instructions.lower() or "deleting" in instructions.lower()
        assert "confirmation" in instructions.lower() or "confirm" in instructions.lower()

    def test_pause_over_delete_guidance(self, instructions):
        """Should recommend pausing over deleting creatives."""
        assert "pause" in instructions.lower() or "pausing" in instructions.lower()
        assert "historical data" in instructions.lower() or "preserve" in instructions.lower()

    def test_read_only_note(self, instructions):
        """Must state that creation is done in the UI."""
        assert "Influ2 UI" in instructions or "Influ2 dashboard" in instructions

    def test_no_creation_via_mcp(self, instructions):
        """Must explicitly say cohort/content creation is not available."""
        assert "creation" in instructions.lower()
        assert "UI" in instructions or "dashboard" in instructions


class TestInstructionCompleteness:
    """Verify the instructions contain all required sections."""

    def test_has_key_concepts(self, instructions):
        assert "KEY CONCEPTS" in instructions

    def test_has_typical_workflows(self, instructions):
        assert "TYPICAL WORKFLOWS" in instructions

    def test_has_metrics_rules(self, instructions):
        assert "RULES FOR ANALYZING METRICS" in instructions

    def test_has_target_management_rules(self, instructions):
        assert "RULES FOR TARGET MANAGEMENT" in instructions

    def test_defines_cohort(self, instructions):
        assert "Cohort" in instructions

    def test_defines_target(self, instructions):
        assert "Target" in instructions

    def test_defines_buying_group(self, instructions):
        assert "Buying Group" in instructions

    def test_defines_journey(self, instructions):
        assert "Journey" in instructions
