<?php

declare(strict_types=1);

namespace PortalPeople\sitesetup\controllers\api;

use PortalPeople\sitesetup\Module;
use Yii;
use yii\rest\Controller;

/**
 * @OA\Tag(
 *     name="Site Setup",
 *     description="One-time NGauge installation wizard endpoint"
 * )
 */
class SiteController extends Controller
{
    /**
     * @OA\Get(
     *     path="/v1/site/setup/{email}",
     *     tags={"Site Setup"},
     *     summary="Run one-time NGauge installation",
     *     @OA\Parameter(
     *         name="email",
     *         in="path",
     *         required=true,
     *         @OA\Schema(type="string", format="email")
     *     ),
     *     @OA\Response(response=200, description="Setup complete (or already run)"),
     *     @OA\Response(response=503, description="Module unavailable")
     * )
     */
    public function actionSetup(string $email): void
    {
        /** @var Module|null $module */
        $module = Yii::$app->getModule('site-setup');

        if ($module === null || !method_exists($module, 'getSetupService')) {
            Yii::error('site-setup module unavailable in SiteController::actionSetup', __CLASS__);
            Yii::$app->response->statusCode = 503;
            return;
        }

        $module->getSetupService()->runSetup($email);
    }
}
