#!/usr/bin/php -q
<?php

require_once __DIR__ . '/../vendor/autoload.php';
include_once __DIR__ . '/../config/locale_setup.php';

if (count($argv) != 5){
    echo 'wrong amount of parameters for this command';?>
    what: ./gvconsole create-user {username} {email} {password} {user role name}
    usage example: ./gvconsole create-user admin admin@admin.com complexPassword admin
    <?php
    exit(0);
}
$config = new \Gvera\Helpers\config\Config(__DIR__ . '/../config/config.yml');
\Gvera\Cache\Cache::setConfig($config);
$diContainer = getDiContainer();
$userService = $diContainer->get('userService');
$hashedPass = $userService->generatePassword($argv[3]);


try {
    $validationService = new \Gvera\Helpers\validation\ValidationService();
    $validationService->validate($argv[2], [new \Gvera\Helpers\validation\EmailValidationStrategy()]);
    $entityManager = $diContainer->get('entityManager');

    $role = $entityManager->getRepository(
            \Gvera\Models\__CG__\Gvera\Models\UserRole::class
    )->findOneBy(['name' => $argv[4]]);

    if (!isset($role)) {
        echo 'Role to be applied to the user cannot be found, please create the role before creating the user';
        exit(0);
    }

    $command = new \Gvera\Commands\CreateNewUserCommand($config, $entityManager);
    $command->setName($argv[1]);
    $command->setEmail($argv[2]);
    $command->setPassword($hashedPass);
    $command->setRoleId($role->getId());
    $command->execute();
} catch (Exception $e) {
    echo $e->getMessage();
}

echo 'user generated successfully';

function getDiContainer() {
    $diContainer = new \Gvera\Helpers\dependencyInjection\DIContainer();
    $diRegistry = new \Gvera\Helpers\dependencyInjection\DIRegistry($diContainer, __DIR__.'/../config/ioc.yml');
    $diRegistry->registerObjects();
    return $diContainer;
}