<?php

declare(strict_types=1);

namespace PortalPeople\booking\controllers\api;

use Yii;
use yii\rest\Controller;

/**
 * @OA\Tag(
 *     name="Bookings",
 *     description="Board and equipment availability checking for surf/sport bookings"
 * )
 */
class BookingController extends Controller
{
    /**
     * @OA\Post(
     *     path="/v1/booking/check",
     *     tags={"Bookings"},
     *     summary="Check board/equipment availability for a date range",
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(
     *             type="object",
     *             @OA\Property(property="bookingbe", type="object",
     *                 @OA\Property(property="source", type="string"),
     *                 @OA\Property(property="productid", type="integer"),
     *                 @OA\Property(property="Center", type="integer"),
     *                 @OA\Property(property="sport", type="integer"),
     *                 @OA\Property(property="startdatea", type="object"),
     *                 @OA\Property(property="enddatea", type="object")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="Availability table HTML partial"),
     *     @OA\Response(response=503, description="Module unavailable")
     * )
     */
    public function actionCheck(): mixed
    {
        /** @var \PortalPeople\booking\Module|null $module */
        $module = Yii::$app->getModule('booking');

        if ($module === null || !method_exists($module, 'getAvailabilityService')) {
            Yii::error('booking module unavailable in api/BookingController::actionCheck', __CLASS__);
            Yii::$app->response->statusCode = 503;
            return null;
        }

        return $module->getAvailabilityService()->check(
            Yii::$app->request->getBodyParams()
        );
    }
}
