<?php

declare(strict_types=1);

namespace PortalPeople\schema\controllers\api;

use OpenApi\Annotations as OA;
use Yii;
use yii\filters\auth\HttpBearerAuth;
use yii\rest\Controller;

/**
 * @OA\Tag(name="Schema", description="DB schema synchronisation (admin use only)")
 */
class SchemaController extends Controller
{
    public function behaviors(): array
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => HttpBearerAuth::class,
        ];
        return $behaviors;
    }

    /** Upload a schema JSON file and sync the DB. */
    public function actionUploadschema(): mixed
    {
        /** @var \PortalPeople\schema\Module $module */
        $module = Yii::$app->getModule('ngauge-schema');

        return $module->getSchemaService()->uploadschema();
    }

    /**
     * Check the DB schema against the stored JSON definition.
     *
     * @param string $connection Database connection component name.
     */
    public function actionCheckschema(string $connection): mixed
    {
        /** @var \PortalPeople\schema\Module $module */
        $module = Yii::$app->getModule('ngauge-schema');

        return $module->getSchemaService()->checkschema($connection);
    }
}
