<?php

namespace Modules\Main\Http\Controllers\Company;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Main\Models\Company\CompanyAdmin;
use Modules\Main\Models\Agent\Agent;

use App\Notifications\UserRegistered;

class ResetAgentPasswordController extends Controller
{
    public function __invoke(Request $request, Agent $agent)
    {
        $admin = CompanyAdmin::where('user_id', auth()->id());
        if (!$admin->count())
        {
            abort(404);
            return;
        }
        $admin = $admin->first();
        if ($agent->company_id != $admin->company_id)
        {
            return back();
        }
        $user = $agent->user;

        if ($request->sendToEmail)
        {
            $user->setForcedPassword();
        }
        else
        {
            $password = $user->setForcedPassword(false);
            return back()->with('success', 'Пароль для пользователя "' . $user->name . '" сброшен. Новый пароль: ' . $password);
        }
        return back();
    }
}
