import pytest
from pydantic import ValidationError

from backend.magic_plan.magic_plan_trigger_request import MagicPlanTriggerRequest


def test_valid_payload_with_address_only() -> None:
    # Arrange
    payload = {"address": "123 High St London SW1A 1AA", "hubspot_deal_id": "123456789"}
    # Act
    req = MagicPlanTriggerRequest.model_validate(payload)
    # Assert
    assert req.address == "123 High St London SW1A 1AA"
    assert req.hubspot_deal_id == "123456789"
    assert req.uprn is None


def test_valid_payload_with_uprn() -> None:
    # Arrange
    payload = {"address": "123 High St London SW1A 1AA", "hubspot_deal_id": "123456789", "uprn": "100023336956"}
    # Act
    req = MagicPlanTriggerRequest.model_validate(payload)
    # Assert
    assert req.uprn == "100023336956"


def test_missing_address_raises() -> None:
    # Arrange
    payload = {"hubspot_deal_id": "123456789", "uprn": "100023336956"}
    # Act / Assert
    with pytest.raises(ValidationError):
        MagicPlanTriggerRequest.model_validate(payload)


def test_extra_fields_ignored() -> None:
    # Arrange
    payload = {"address": "123 High St London SW1A 1AA", "hubspot_deal_id": "123456789", "unknown_field": "whatever"}
    # Act
    req = MagicPlanTriggerRequest.model_validate(payload)
    # Assert
    assert req.address == "123 High St London SW1A 1AA"


def test_missing_hubspot_deal_id_raises() -> None:
    # Arrange
    payload = {"address": "123 High St London SW1A 1AA"}
    # Act / Assert
    with pytest.raises(ValidationError):
        MagicPlanTriggerRequest.model_validate(payload)
