<?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;
use WP_REST_Request;

defined( 'ABSPATH' ) || exit;

class ChangePaymentTypeRequest extends AbstractRequest
{
    public function __construct(
        private readonly string $itemHash,
        private readonly int $paymentTypeId
    ) {
    }

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

        return new self(
            itemHash: $params['itemHash'],
            paymentTypeId: (int) ($params['paymentTypeId'] ?? $params['paymentType'])
        );
    }

    public static function getValidationRules(): array
    {
        return [
            'itemHash' => [
                'required' => true,
                'validate_callback' => fn($value) => is_string($value),
                'sanitize_callback' => 'sanitize_text_field',
            ],
            'paymentTypeId' => self::positiveIntRule(),
        ];
    }

    public function getItemHash(): string
    {
        return $this->itemHash;
    }

    public function getPaymentTypeId(): int
    {
        return $this->paymentTypeId;
    }
}
