<?php
/**
 * BookiFlex Cart Extension
 *
 * @package    BookiFlex
 * @subpackage Cart
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Cart\Api\Request;

use BookiFlex\Core\Api\Request\AbstractRequest;

defined( 'ABSPATH' ) || exit;

class UpdateCartItemRequest extends AbstractRequest
{
    public function __construct(
        private readonly string $hash,
        private readonly int $quantity
    ) {
    }

    public static function fromWpRestRequest(\WP_REST_Request $request): self
    {
        $params = $request->get_json_params();

        return new self(
            hash: $request->get_param('hash'),
            quantity: (int) $params['quantity']
        );
    }

    public static function getValidationRules(): array
    {
        return [
            'hash' => [
                'required' => true,
                'validate_callback' => fn($value) => is_string($value) && preg_match('/^[a-zA-Z0-9]+$/', $value),
                'sanitize_callback' => 'sanitize_text_field',
            ],
            'quantity' => [
                'required' => true,
                'validate_callback' => fn($value) => is_numeric($value) && (int)$value >= 0,
                'sanitize_callback' => 'absint',
            ],
        ];
    }

    public function getHash(): string
    {
        return $this->hash;
    }

    public function getQuantity(): int
    {
        return $this->quantity;
    }
}
