<?php

namespace classes;

use objects\orders_o;

class customer_product_rule_service
{
    public const BLOCK_MESSAGE = 'This product is not allowed for the selected customer';

    /**
     * @return array{rule:string,rules:list<string>,collections:list<int>,product_id:int,code:string,message:string}|null
     */
    public function firstViolationForOrderItem(int $orderId, int $productId, ?int $relatedItemId): ?array
    {
        $order = (new orders_o())->getOrderById($orderId);
        if (!$order->exists()) {
            return null;
        }
        $violation = (new customer_rule_product_restriction_service())->violationForCustomerProduct(
            (int)$order->customer_id->value(),
            $productId
        );
        if ($violation === null) {
            return null;
        }

        // Keep the singular key during the API migration for existing invoice
        // and logging consumers while also returning every matching rule.
        return ['rule' => (string)$violation['rules'][0]] + $violation;
    }

}
