<?php

namespace App\Exceptions;

use RuntimeException;

/**
 * A checkout/return/damage payload named a lot or a serial that does not
 * exist, does not belong to the stated product/branch, or carries no serials.
 *
 * The typed $field exists so Filament pages can map the failure onto the
 * exact form field (stock_batch_id vs serial_ids) instead of sniffing the
 * message text. Extends RuntimeException so generic UI catches keep working.
 */
final class StockReferenceException extends RuntimeException
{
    public const FIELD_SERIAL = 'serial_ids';

    public const FIELD_LOT = 'stock_batch_id';

    public const FIELD_EXPIRY = 'expiry_date';

    private function __construct(
        public readonly string $field,
        public readonly ?int $productId,
        string $message,
    ) {
        parent::__construct($message);
    }

    public static function lot(?int $productId, string $message): self
    {
        return new self(self::FIELD_LOT, $productId, $message);
    }

    public static function serial(?int $productId, string $message): self
    {
        return new self(self::FIELD_SERIAL, $productId, $message);
    }

    public static function expiry(?int $productId, string $message): self
    {
        return new self(self::FIELD_EXPIRY, $productId, $message);
    }
}
