from api.v1.modules.a76.general_catalogs.fractions.tariff_fractions.service import (
    TariffFractionMapper,
)
from api.v1.modules.a76.general_catalogs.fractions.us_tariff_fractions.dto import (
    USTariffFractionResponseDTO,
)
from api.v1.modules.sitar.fracciones_usa.schemas import FraccionesUSAResponse


def test_to_domain_usa_keeps_separate_code_and_fraction():
    row = FraccionesUSAResponse(
        CONSECUTIVO=10,
        FRACCION_SIN_PUNTO="1234567890",
        FRACCION_CON_PUNTO="1234.56.78.90",
        DESCRIPCION="Test",
        UNIDADCANTIDAD="KG",
        TARIFA1="5%",
        TARIFA2="0%",
    )

    mapped = TariffFractionMapper.to_domain_usa(row)

    assert mapped.code == "1234567890"
    assert mapped.fraction == "1234.56.78.90"


def test_to_domain_usa_formats_fraction_when_only_code_available():
    row = FraccionesUSAResponse(
        CONSECUTIVO=11,
        FRACCION_SIN_PUNTO="9876543210",
        FRACCION_CON_PUNTO=None,
        FRACCION_MOSTRAR=None,
        DESCRIPCION="Fallback",
        UNIDADCANTIDAD="PZA",
        TARIFA1="7.5%",
        TARIFA2="0%",
    )

    mapped = TariffFractionMapper.to_domain_usa(row)

    assert mapped.code == "9876543210"
    assert mapped.fraction == "9876.54.32.10"


def test_us_response_dto_preserves_fraction_when_provided():
    dto = USTariffFractionResponseDTO.model_validate(
        {
            "id": 1,
            "code": "1111.22.33.44",
            "fraction": "1111.22.33.44",
            "description": "DTO test",
            "created_at": "2026-01-01T00:00:00Z",
            "updated_at": "2026-01-01T00:00:00Z",
        }
    )
    assert dto.code == "1111223344"
    assert dto.fraction == "1111.22.33.44"
