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

declare(strict_types=1);

namespace BookiFlex\Extensions\Payment\Api\Request;

use BookiFlex\Core\Api\Request\AbstractRequest;
use Symfony\Component\Uid\Uuid;
use WP_REST_Request;

defined( 'ABSPATH' ) || exit;

class GetPublicTokenRequest extends AbstractRequest
{
    public function __construct(
        private readonly string $reservationSid,
        private readonly bool $redirect = false
    ) {
    }

    public static function fromWpRestRequest(WP_REST_Request $request): self
    {
        return new self(
            reservationSid: (string) $request->get_param('reservationSid'),
            redirect: (bool) $request->get_param('redirect')
        );
    }

    public static function getValidationRules(): array
    {
        return [
            'reservationSid' => [
                'required'          => true,
                'validate_callback' => fn($param) => Uuid::isValid($param),
                'sanitize_callback' => [self::class, 'sanitizeString'],
            ],
            'redirect' => [
                'required'          => false,
                'default'           => false,
                'validate_callback' => fn($param) => is_bool($param) || is_numeric($param) || is_string($param),
                'sanitize_callback' => [self::class, 'sanitizeBool'],
            ],
        ];
    }

    public function toArray(): array
    {
        return [
            'reservationSid' => $this->reservationSid,
            'redirect' => $this->redirect,
        ];
    }

    public function getReservationSid(): string
    {
        return $this->reservationSid;
    }

    public function isRedirect(): bool
    {
        return $this->redirect;
    }
}
