<?php

declare(strict_types=1);

namespace PortalPeople\users\services;

use common\models\Loginform;

/**
 * AuthService
 *
 * Wraps the NGauge login flow previously handled directly in
 * api/modules/v1/controllers/UserController::actionLogin().
 */
class AuthService
{
    /**
     * Attempt to log in with the given parameters.
     *
     * Returns ['access_token' => ...] on success, or an array of validation
     * errors on failure.
     *
     * @param array<string, mixed> $params  Body params (username, password, etc.)
     * @return array<string, mixed>
     */
    public function login(array $params): array
    {
        $model = new Loginform();

        if ($model->load($params, '') && $model->login()) {
            return ['access_token' => $model->login()];
        }

        return $model->getFirstErrors();
    }
}
