from __future__ import annotations

import pytest
from datetime import datetime
from uuid import uuid4

from v1.friendships.schemas import (
    UserBasicInfo,
    FriendRequestCreate,
    FriendRequestResponse,
    FriendResponse,
    FriendRequestAction,
)


def test_user_basic_info_maps_fields() -> None:
    user = UserBasicInfo(id="user-1", username="alice", avatar_url=None)

    assert user.id == "user-1"
    assert user.username == "alice"
    assert user.avatar_url is None


def test_user_basic_info_with_avatar() -> None:
    user = UserBasicInfo(
        id="user-2", username="bob", avatar_url="https://example.com/avatar.png"
    )

    assert user.avatar_url == "https://example.com/avatar.png"


def test_friend_request_create_valid() -> None:
    target_id = uuid4()
    request = FriendRequestCreate(
        target_user_id=target_id, content="Hi, let's be friends!"
    )

    assert request.target_user_id == target_id
    assert request.content == "Hi, let's be friends!"


def test_friend_request_create_without_content() -> None:
    target_id = uuid4()
    request = FriendRequestCreate(target_user_id=target_id, content=None)

    assert request.target_user_id == target_id
    assert request.content is None


def test_friend_request_create_content_max_length() -> None:
    target_id = uuid4()
    with pytest.raises(Exception):
        FriendRequestCreate(target_user_id=target_id, content="x" * 201)


def test_friend_request_response_maps_fields() -> None:
    sender = UserBasicInfo(id="user-1", username="alice", avatar_url=None)
    recipient = UserBasicInfo(id="user-2", username="bob", avatar_url=None)
    request_id = uuid4()
    created = datetime(2026, 1, 15, 10, 30, 0)

    response = FriendRequestResponse(
        id=request_id,
        sender=sender,
        recipient=recipient,
        content="Hello!",
        status="pending",
        created_at=created,
    )

    assert response.id == request_id
    assert response.sender.username == "alice"
    assert response.recipient.username == "bob"
    assert response.status == "pending"
    assert response.created_at == created


def test_friend_response_maps_fields() -> None:
    friend_user = UserBasicInfo(id="user-2", username="bob", avatar_url=None)
    request_id = uuid4()
    created = datetime(2026, 1, 15, 10, 30, 0)
    accepted = datetime(2026, 1, 16, 12, 0, 0)

    response = FriendResponse(
        id=request_id,
        friend=friend_user,
        status="active",
        created_at=created,
        accepted_at=accepted,
    )

    assert response.id == request_id
    assert response.friend.username == "bob"
    assert response.status == "active"
    assert response.accepted_at == accepted


def test_friend_response_accepted_at_optional() -> None:
    friend_user = UserBasicInfo(id="user-2", username="bob", avatar_url=None)
    request_id = uuid4()
    created = datetime(2026, 1, 15, 10, 30, 0)

    response = FriendResponse(
        id=request_id,
        friend=friend_user,
        status="active",
        created_at=created,
        accepted_at=None,
    )

    assert response.accepted_at is None


def test_friend_request_action_no_fields() -> None:
    action = FriendRequestAction()
    assert action.model_dump() == {}
