"""Tests for persona parser."""

import pytest
from domain_vigil.parsers.persona_parser import PersonaParser
from domain_vigil.core.models import Severity


def test_parse_simple_persona():
    """Test parsing a simple persona."""
    data = {
        "persona": "shopper",
        "description": "A customer who shops",
        "deal_breakers": [
            {
                "id": "lost_cart",
                "description": "Cart items must not disappear",
                "severity": "critical"
            }
        ]
    }

    persona = PersonaParser.parse_dict(data)

    assert persona.name == "shopper"
    assert persona.description == "A customer who shops"
    assert len(persona.deal_breakers) == 1
    assert persona.deal_breakers[0].id == "lost_cart"
    assert persona.deal_breakers[0].severity == Severity.CRITICAL


def test_parse_persona_with_abilities():
    """Test parsing persona with abilities."""
    data = {
        "persona": "shopper",
        "deal_breakers": [],
        "abilities": [
            {
                "id": "browse",
                "description": "Browse products"
            }
        ]
    }

    persona = PersonaParser.parse_dict(data)

    assert len(persona.abilities) == 1
    assert persona.abilities[0].id == "browse"


def test_parse_missing_persona_field():
    """Test error when persona field is missing."""
    data = {"description": "Test"}

    with pytest.raises(ValueError, match="Missing required field: 'persona'"):
        PersonaParser.parse_dict(data)


def test_parse_invalid_severity():
    """Test error when severity is invalid."""
    data = {
        "persona": "test",
        "deal_breakers": [
            {
                "id": "test",
                "description": "Test",
                "severity": "invalid"
            }
        ]
    }

    with pytest.raises(ValueError, match="Invalid severity"):
        PersonaParser.parse_dict(data)


def test_to_dict_roundtrip():
    """Test converting persona to dict and back."""
    original_data = {
        "persona": "shopper",
        "description": "A customer",
        "deal_breakers": [
            {
                "id": "lost_cart",
                "description": "Cart must not disappear",
                "severity": "critical",
                "examples": ["Example 1"],
                "learned_patterns": []
            }
        ],
        "abilities": [
            {
                "id": "browse",
                "description": "Browse products",
                "examples": []
            }
        ],
        "metadata": {"domain": "ecommerce"}
    }

    persona = PersonaParser.parse_dict(original_data)
    converted = PersonaParser.to_dict(persona)

    assert converted["persona"] == original_data["persona"]
    assert converted["description"] == original_data["description"]
    assert len(converted["deal_breakers"]) == len(original_data["deal_breakers"])
    assert len(converted["abilities"]) == len(original_data["abilities"])
