from typing import List

from sqlalchemy.orm import Session

from api.v1.modules.a76.general_catalogs.units_of_measure.models import UnitOfMeasure
from api.v1.modules.a76.items.line_quantities.models import LineQuantity
from api.v1.modules.a76.items.models import LineItem
from core.exceptions import ErrorCollector


def review_weights_kgs(
    db: Session,
    lines: List[LineItem],
    tenant_id: str,
    company_id: str,
    errors: ErrorCollector,
) -> None:
    """
    Validates that, for lines whose unit of measure is KGS, the net weight
    equals the import quantity.

    Ported from legacy REVISA_CANT_vs_PESONETO_KGS routine.
    """
    # Resolve the id of the KGS unit of measure for this tenant/company
    kgs_uom = (
        db.query(UnitOfMeasure)
        .filter(
            UnitOfMeasure.code == "KGS",
            UnitOfMeasure.tenant_id == tenant_id,
            UnitOfMeasure.company_id == company_id,
        )
        .first()
    )
    if kgs_uom is None:
        # No KGS unit defined for this tenant — nothing to validate
        return

    for line in lines:
        if line.unit_of_measure != kgs_uom.id:
            continue
        if line.quantity is None:
            continue

        net_weight = line.quantity.net_weight
        qty = line.quantity.quantity

        if net_weight is None or qty is None or net_weight != qty:
            errors.add_error(
                field=f"line[{line.line_number}].net_weight",
                message=(
                    f"Existe diferencia entre la cantidad y el Peso Neto, "
                    f"la cantidad es de {qty} KGS y el Peso Neto es de {net_weight} KGS."
                ),
                solution=["Igualar el Peso Neto con la cantidad a importar."],
                code="PESO_NETO_KGS",
            )


def review_weights_lbs(
    db: Session,
    lines: List[LineItem],
    tenant_id: str,
    company_id: str,
    errors: ErrorCollector,
) -> None:
    """
    Validates that, for lines whose unit of measure is LBS, the net weight
    equals the import quantity.

    Ported from legacy REVISA_CANT_vs_PESONETO_LBS routine.
    """
    lbs_uom = (
        db.query(UnitOfMeasure)
        .filter(
            UnitOfMeasure.code == "LBS",
            UnitOfMeasure.tenant_id == tenant_id,
            UnitOfMeasure.company_id == company_id,
        )
        .first()
    )
    if lbs_uom is None:
        return

    for line in lines:
        if line.unit_of_measure != lbs_uom.id:
            continue
        if line.quantity is None:
            continue

        net_weight = line.quantity.net_weight
        qty = line.quantity.quantity

        if net_weight is None or qty is None or net_weight != qty:
            errors.add_error(
                field=f"line[{line.line_number}].net_weight",
                message=(
                    f"Existe diferencia entre la cantidad y el Peso Neto, "
                    f"la cantidad es de {qty} LBS y el Peso Neto es de {net_weight} LBS."
                ),
                solution=["Igualar el Peso Neto con la cantidad a importar."],
                code="PESO_NETO_LBS",
            )
