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

declare(strict_types=1);

namespace BookiFlex\Core\Api\Request\Reservation;

use BookiFlex\Core\Api\Request\AbstractRequest;
use BookiFlex\Core\Model\Reservation\ReservationStatus;
use WP_REST_Request;

defined( 'ABSPATH' ) || exit;

class ChangeStatusRequest extends AbstractRequest
{
    public function __construct(
        private readonly int $id,
        private readonly string $status,
        private readonly ?string $reason = null
    ) {
    }

    /**
     * Create request from WP REST Request
     */
    public static function fromWpRestRequest(WP_REST_Request $request): self
    {
        $params = $request->get_json_params();

        return new self(
            id: (int) $params['id'],
            status: $params['status'],
            reason: isset($params['reason']) ? (string) $params['reason'] : null
        );
    }

    /**
     * Get validation rules for WP REST API
     */
    public static function getValidationRules(): array
    {
        return [
            'id' => self::positiveIntRule(required: true),
            'status' => [
                'required' => true,
                'type' => 'string',
                'enum' => [
                    ReservationStatus::STATUS_CANCELLED,
                    ReservationStatus::STATUS_CONFIRMED,
                    ReservationStatus::STATUS_WAITING_PAYMENT,
                    ReservationStatus::STATUS_NO_SHOW,
                    ReservationStatus::STATUS_IN_HOUSE,
                    ReservationStatus::STATUS_COMPLETED
                ],
                'validate_callback' => 'rest_validate_request_arg',
                'sanitize_callback' => fn($value) => self::sanitizeString($value),
            ],
            'reason' => [
                'required' => false,
                'type' => 'string',
                'maxLength' => 500,
                'validate_callback' => 'rest_validate_request_arg',
                'sanitize_callback' => fn($value) => self::sanitizeString($value),
            ],
        ];
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function getStatus(): string
    {
        return $this->status;
    }

    public function getReason(): ?string
    {
        return $this->reason;
    }
}
