from datetime import datetime, timezone

from app.models.crm_schemas import (
    Contact,
    Conversation,
    Message,
    TelegramTopicMap,
    ZaloAccount,
)


def test_zalo_account_schema():
    data = {
        "id": "zalo-acc-1234",
        "name": "Sơn - Giám Đốc",
        "phone_number": "0987654321",
    }
    acc = ZaloAccount(**data)
    assert acc.id == "zalo-acc-1234"
    assert acc.name == "Sơn - Giám Đốc"
    assert acc.phone_number == "0987654321"
    assert acc.created_at is None


def test_contact_schema():
    data = {
        "id": "contact-789",
        "name": "Sơn - Giám Đốc",
        "phone_number": "0987654321",
        "is_internal": True,
    }
    contact = Contact(**data)
    assert contact.id == "contact-789"
    assert contact.name == "Sơn - Giám Đốc"
    assert contact.is_internal is True


def test_conversation_schema():
    data = {
        "id": "conv-456",
        "zalo_account_id": "zalo-acc-1234",
        "contact_id": "contact-789",
        "is_internal": True,
    }
    conv = Conversation(**data)
    assert conv.id == "conv-456"
    assert conv.zalo_account_id == "zalo-acc-1234"
    assert conv.contact_id == "contact-789"
    assert conv.is_internal is True


def test_message_schema():
    now = datetime.now(timezone.utc)
    data = {
        "id": "msg-001",
        "conversation_id": "conv-456",
        "sender_type": "contact",
        "sender_id": "contact-789",
        "content": "Báo cáo tiến độ dự án 123.",
        "timestamp": now,
    }
    msg = Message(**data)
    assert msg.id == "msg-001"
    assert msg.conversation_id == "conv-456"
    assert msg.sender_type == "contact"
    assert msg.sender_id == "contact-789"
    assert msg.content == "Báo cáo tiến độ dự án 123."
    assert msg.timestamp == now


def test_telegram_topic_map_schema():
    data = {
        "id": "map-111",
        "conversation_id": "conv-456",
        "telegram_topic_id": "topic-888",
        "telegram_group_id": "group-999",
    }
    t_map = TelegramTopicMap(**data)
    assert t_map.id == "map-111"
    assert t_map.conversation_id == "conv-456"
    assert t_map.telegram_topic_id == "topic-888"
    assert t_map.telegram_group_id == "group-999"
