"""Validator unit tests."""
from __future__ import annotations

import pytest

from app.validators import (
    ValidationError,
    extract_template_variables,
    render_template,
    validate_channel,
    validate_contact,
    validate_message_send,
    validate_otp_request,
    validate_provider_payload,
    validate_template,
)


def test_validate_channel():
    assert validate_channel("sms") == "sms"
    with pytest.raises(ValidationError):
        validate_channel("carrier-pigeon")


def test_validate_provider_payload():
    validate_provider_payload(
        {"name": "P", "provider_kind": "mock", "channel": "sms", "priority": 10}
    )
    with pytest.raises(ValidationError):
        validate_provider_payload({"name": "", "provider_kind": "mock", "channel": "sms"})


def test_template_render():
    assert extract_template_variables("Hi {{name}} {{code}}") == ["code", "name"]
    assert render_template("Hi {{name}}", {"name": "Ali"}) == "Hi Ali"
    with pytest.raises(ValidationError):
        render_template("Hi {{name}}", {})


def test_validate_template_variables_declared():
    with pytest.raises(ValidationError):
        validate_template(
            {
                "template_key": "t",
                "name": "T",
                "channel": "sms",
                "body": "Hi {{name}}",
                "variables": [],
            }
        )


def test_validate_contact_and_message():
    validate_contact({"phone": "+989121234567"})
    with pytest.raises(ValidationError):
        validate_contact({})
    validate_message_send(
        {"channel": "sms", "to_address": "+989121234567", "body": "x"}
    )
    validate_otp_request({"destination": "+989121234567", "channel": "sms"})
