# app/modules/tenancy/tests/unit/test_user_account_schema.py
"""
Unit tests for user account schemas.

Tests validation rules for UserPasswordChange and UserAccountUpdate.
"""

import pytest
from pydantic import ValidationError

from app.modules.tenancy.schemas.user_account import (
    UserAccountUpdate,
    UserPasswordChange,
)


@pytest.mark.unit
@pytest.mark.schema
class TestUserPasswordChangeSchema:
    """Tests for UserPasswordChange validation."""

    def test_valid_password(self):
        schema = UserPasswordChange(
            current_password="oldpass123",  # noqa: SEC001
            new_password="newpass456",  # noqa: SEC001
            confirm_password="newpass456",  # noqa: SEC001
        )
        assert schema.new_password == "newpass456"

    def test_password_too_short(self):
        with pytest.raises(ValidationError) as exc_info:
            UserPasswordChange(
                current_password="old",  # noqa: SEC001
                new_password="short1",  # noqa: SEC001
                confirm_password="short1",  # noqa: SEC001
            )
        assert "at least 8" in str(exc_info.value).lower() or "min_length" in str(
            exc_info.value
        ).lower()

    def test_password_no_digit(self):
        with pytest.raises(ValidationError) as exc_info:
            UserPasswordChange(
                current_password="oldpass123",  # noqa: SEC001
                new_password="nodigitss",  # noqa: SEC001
                confirm_password="nodigitss",  # noqa: SEC001
            )
        assert "digit" in str(exc_info.value).lower()

    def test_password_no_letter(self):
        with pytest.raises(ValidationError) as exc_info:
            UserPasswordChange(
                current_password="oldpass123",  # noqa: SEC001
                new_password="12345678",  # noqa: SEC001
                confirm_password="12345678",  # noqa: SEC001
            )
        assert "letter" in str(exc_info.value).lower()


@pytest.mark.unit
@pytest.mark.schema
class TestUserAccountUpdateSchema:
    """Tests for UserAccountUpdate validation."""

    def test_valid_update(self):
        schema = UserAccountUpdate(
            first_name="John",
            last_name="Doe",
            email="john@example.com",
            preferred_language="en",
        )
        assert schema.first_name == "John"

    def test_partial_update(self):
        schema = UserAccountUpdate(first_name="John")
        assert schema.first_name == "John"
        assert schema.last_name is None
        assert schema.email is None

    def test_email_validation(self):
        with pytest.raises(ValidationError):
            UserAccountUpdate(email="not-an-email")

    def test_language_validation(self):
        with pytest.raises(ValidationError):
            UserAccountUpdate(preferred_language="xx")

    def test_valid_languages(self):
        for lang in ("en", "fr", "de", "lb"):
            schema = UserAccountUpdate(preferred_language=lang)
            assert schema.preferred_language == lang
