<?php

declare(strict_types=1);

namespace PortalPeople\users;

use PortalPeople\users\services\AuthService;
use yii\base\BootstrapInterface;
use yii\base\Module as BaseModule;
use yii\web\Application as WebApplication;

/**
 * NGauge Users Module
 *
 * Provides user authentication endpoints, extracted from
 * NGauge_Base/api/modules/v1/controllers/UserController.
 */
class Module extends BaseModule implements BootstrapInterface
{
    public $controllerNamespace = 'PortalPeople\\users\\controllers';
    public string $moduleId = 'ngauge-users';

    private ?AuthService $authService = null;

    public function getAuthService(): AuthService
    {
        if ($this->authService === null) {
            $this->authService = new AuthService();
        }

        return $this->authService;
    }

    public function bootstrap($app): void
    {
        $this->id = $this->moduleId;

        if (!isset($app->getModules()[$this->id])) {
            $app->setModule($this->id, $this);
        }

        if ($app instanceof WebApplication && $app->has('urlManager')) {
            $app->getUrlManager()->addRules([
                'POST v1/user/login' => 'ngauge-users/api/user/login',
            ], false);
        }
    }
}
