import pytest
import json


class TestSystemPromptContainsNewFields:
    def test_prompt_has_pricing_schema(self):
        from app.services.prompts import SYSTEM_PROMPT_AURA_BUILDER
        prompt_lower = SYSTEM_PROMPT_AURA_BUILDER.lower()
        assert "pricing" in prompt_lower, "System prompt must mention pricing"
        assert "pricing_tiers" in SYSTEM_PROMPT_AURA_BUILDER or "tiers" in prompt_lower, "Must define pricing tiers schema"

    def test_prompt_has_how_it_works_schema(self):
        from app.services.prompts import SYSTEM_PROMPT_AURA_BUILDER
        prompt_lower = SYSTEM_PROMPT_AURA_BUILDER.lower()
        assert "how_it_works" in SYSTEM_PROMPT_AURA_BUILDER or ("how" in prompt_lower and "work" in prompt_lower), "Must define how_it_works schema"

    def test_prompt_has_comparison_schema(self):
        from app.services.prompts import SYSTEM_PROMPT_AURA_BUILDER
        assert "comparison" in SYSTEM_PROMPT_AURA_BUILDER.lower(), "Must define comparison section schema"
        assert "comparison_table" in SYSTEM_PROMPT_AURA_BUILDER, "Must use exact key name comparison_table"

    def test_comparison_schema_defines_headers_and_rows(self):
        from app.services.prompts import SYSTEM_PROMPT_AURA_BUILDER
        assert "headers" in SYSTEM_PROMPT_AURA_BUILDER, "comparison_table must define headers field"
        assert "rows" in SYSTEM_PROMPT_AURA_BUILDER, "comparison_table must define rows field"


class TestComprehensiveBlueprintIncludesNewSections:
    def test_comprehensive_has_pricing(self):
        from app.services.blueprint import get_default_blueprint
        bp = get_default_blueprint("comprehensive")
        enabled = [s["key"] for s in bp["sections"] if s["enabled"]]
        assert "pricing" in enabled

    def test_comprehensive_has_how_it_works(self):
        from app.services.blueprint import get_default_blueprint
        bp = get_default_blueprint("comprehensive")
        enabled = [s["key"] for s in bp["sections"] if s["enabled"]]
        assert "how_it_works" in enabled

    def test_comprehensive_has_comparison(self):
        from app.services.blueprint import get_default_blueprint
        bp = get_default_blueprint("comprehensive")
        enabled = [s["key"] for s in bp["sections"] if s["enabled"]]
        assert "comparison" in enabled


class TestTranslateSiteCopyHandlesNewFields:
    def test_translates_pricing_nested(self):
        from app.services.aura import _translate_site_copy_output
        copy = {"pricing": {"tiers": [{"name": "Basic", "price": "$9"}]}}
        result = _translate_site_copy_output(copy)
        assert "pricing_tiers" in result
        assert result["pricing_tiers"][0]["name"] == "Basic"

    def test_translates_how_it_works_nested(self):
        from app.services.aura import _translate_site_copy_output
        copy = {"how_it_works": {"steps": [{"title": "Step 1", "description": "Do this"}]}}
        result = _translate_site_copy_output(copy)
        assert "how_it_works_steps" in result
        assert result["how_it_works_steps"][0]["title"] == "Step 1"

    def test_translates_comparison_nested(self):
        from app.services.aura import _translate_site_copy_output
        copy = {"comparison": {"comparison_table": {"headers": ["A", "B"], "rows": [["x", "y"]]}}}
        result = _translate_site_copy_output(copy)
        assert "comparison_table" in result
        assert result["comparison_table"]["headers"] == ["A", "B"]


class TestSiteCopyValidatorAcceptsNewFields:
    def test_validator_preserves_pricing_tiers(self):
        from app.services.validators import validate_site_copy
        copy = {"headline": "Test", "pricing_tiers": [{"name": "Basic", "price": "$9/mo", "features": ["Feature 1"]}]}
        result, report = validate_site_copy(copy, auto_repair=True)
        assert "pricing_tiers" in result

    def test_validator_preserves_how_it_works(self):
        from app.services.validators import validate_site_copy
        copy = {"headline": "Test", "how_it_works_steps": [{"title": "Step 1", "description": "Do this"}]}
        result, report = validate_site_copy(copy, auto_repair=True)
        assert "how_it_works_steps" in result

    def test_validator_preserves_comparison_table(self):
        from app.services.validators import validate_site_copy
        copy = {"headline": "Test", "comparison_table": {"headers": ["Feature", "Us", "Them"], "rows": [["Speed", "Fast", "Slow"]]}}
        result, report = validate_site_copy(copy, auto_repair=True)
        assert "comparison_table" in result
