"""Approved Phase 3B integration hardening of the global part update service."""
from django.core.exceptions import ValidationError

from .models import SerializedStockUnit, StockMovement


def require_policy_editable(spare_part):
    # Caller holds SparePart FOR UPDATE. Unit creation and movement posting hold
    # FOR SHARE on that same row until commit. Drafts are deliberately absent.
    if SerializedStockUnit.objects.filter(spare_part=spare_part).exists() or StockMovement.objects.filter(spare_part=spare_part).exists():
        raise ValidationError({"serialization_policy": "Serialized units or posted inventory history permanently lock this policy."})
