from typing import List

from src.domain.entities.kw_capping import KWCappingRecord
from src.domain.interfaces.kw_capping_repository import KWCappingRepositoryInterface
from src.core.exceptions import ValidationError


class UpdateKWCappingsUseCase:
    """Batch-update max_approved_value for a set of KW tiers.

    Mirrors Laravel's MerchantController::updateKWValues():
    - Only existing KW tiers can be updated; unknown tiers are silently skipped.
    - max_approved_value must be a positive integer for each record.
    """

    def __init__(self, repo: KWCappingRepositoryInterface) -> None:
        self.repo = repo

    def execute(
        self,
        updates: List[KWCappingRecord],
        user_id: int,
    ) -> List[KWCappingRecord]:
        if not updates:
            raise ValidationError("At least one KW record must be provided.")

        for record in updates:
            if record.max_approved_value < 0:
                raise ValidationError(
                    f"max_approved_value for KW={record.kw} must be a non-negative integer."
                )

        return self.repo.bulk_update(updates=updates, user_id=user_id)
