<?php

declare(strict_types=1);

namespace portalpeople\consoletools\controllers\api;

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

/**
 * @OA\Tag(name="ConsoleTools", description="Gii model generation via API (admin use only)")
 */
class ConsoleController extends Controller
{
    public function behaviors(): array
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => HttpBearerAuth::class,
        ];
        return $behaviors;
    }

    /**
     * Generate ActiveRecord models for a given table via Gii.
     *
     * @param string $table
     * @param string $connection
     * @return mixed
     */
    public function actionGiimodel(string $table, string $connection): mixed
    {
        /** @var \portalpeople\consoletools\Module $module */
        $module = Yii::$app->getModule('ngauge-console-tools');

        return $module->getConsoleService()->giimodel($table, $connection);
    }
}
