import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from src.modules.produtos_catalogo.router import _map_to_public_response
from src.modules.produtos_catalogo.schemas import ProdutoCatalogoOut


def test_map_to_public_response_includes_document_id_from_model() -> None:
    produto = ProdutoCatalogoOut(
        id="prod_123",
        collectionId="collection_abc",
        codigo_barras="7898148305100",
        codigo_interno="INT-001",
        nome="Dipirona Sodica",
        descricao="Analgesico",
        preco_consumidor=5.99,
        preco_fabrica=3.99,
        pmc=6.99,
        desconto_comercial=10.0,
        valor_substituicao_tributaria=5.0,
        preco_nf=5.6905,
        laboratorioId="lab_1",
        categoriaId="cat_1",
        subcategoriaId="sub_1",
        createdAt="2024-01-01T00:00:00Z",
        updatedAt="2024-01-01T00:00:00Z",
    )

    response = _map_to_public_response(produto)

    payload = response.model_dump(by_alias=True)
    assert payload["$id"] == "prod_123"
    assert payload["collection_id"] == "collection_abc"


def test_map_to_public_response_includes_document_id_from_dict() -> None:
    raw_produto = {
        "$id": "doc_456",
        "$collectionId": "collection_xyz",
        "codigo_barras": "7898148305101",
        "nome": "Produto Catalogo",
        "preco_consumidor": 10.0,
    }

    response = _map_to_public_response(raw_produto)

    payload = response.model_dump(by_alias=True)
    assert payload["$id"] == "doc_456"
    assert payload["collection_id"] == "collection_xyz"
