<?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 AddCartExtraRequest extends AbstractRequest
{
    public function __construct(
        private readonly int $extraId,
        private readonly string $parentHash,
        private readonly int $quantity,
    ) {
    }

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

        return new self(
            extraId: (int) ($params['extraId'] ?? 0),
            parentHash: (string) ($params['parentHash'] ?? ''),
            quantity: (int) ($params['quantity'] ?? 1),
        );
    }

    public static function getValidationRules(): array
    {
        return [
            'extraId'    => self::positiveIntRule(),
            'parentHash' => [
                'required'          => true,
                'validate_callback' => fn ($value) => is_string($value) && $value !== '',
                'sanitize_callback' => [self::class, 'sanitizeString'],
            ],
            'quantity'   => [
                'required'          => false,
                'default'           => 1,
                'validate_callback' => fn ($value) => is_numeric($value) && (int) $value >= 1,
                'sanitize_callback' => 'absint',
            ],
        ];
    }

    public function getExtraId(): int
    {
        return $this->extraId;
    }

    public function getParentHash(): string
    {
        return $this->parentHash;
    }

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