from uuid import UUID

import pytest

from gaiia import decode_global_id, encode_global_id


def test_roundtrip_known_value():
    type_name = "Account"
    uuid_str = "3c3b1978-6a68-4a13-bdc2-2d51c8ef7519"
    gid = encode_global_id(type_name, uuid_str)
    snake, decoded = decode_global_id(gid)
    assert snake == "account"
    assert decoded == uuid_str


def test_camelcase_type_becomes_snake_case():
    gid = encode_global_id("InventoryItem", "00000000-0000-0000-0000-000000000001")
    snake, _ = decode_global_id(gid)
    assert snake == "inventory_item"


def test_accepts_uuid_object():
    u = UUID("3c3b1978-6a68-4a13-bdc2-2d51c8ef7519")
    gid = encode_global_id("Account", u)
    _, decoded = decode_global_id(gid)
    assert decoded == str(u)


def test_short_id_always_22_chars():
    gid = encode_global_id("Account", "00000000-0000-0000-0000-000000000001")
    _, short = gid.split("_", 1)
    assert len(short) == 22


def test_decode_rejects_garbage():
    with pytest.raises(ValueError):
        decode_global_id("no-underscore-here")
    with pytest.raises(ValueError):
        decode_global_id("account_!!!invalid!!!")


def test_encode_rejects_bad_uuid():
    with pytest.raises(ValueError):
        encode_global_id("Account", "not-a-uuid")
