<?php

namespace Modules\User\Controllers\Admin;

use App\Services\Response\ResponseService;
use Illuminate\Http\RedirectResponse;
//Check the request is valid
use App\Http\Controllers\Controller;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;
use App\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Admin\LoginRequest;
use Modules\User\Services\UserService;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controllers
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your Web screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    // use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::ADMIN;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(
        private UserService $userService
    )
    {
        // $this->middleware('guest')->except('logout');
    }

    /**
     * Show the application dashboard.
     * @return View
     */
    public function index()
    {
        if (Auth::check() && Auth::user()->admin_token){
            return redirect()->route('dashboard');
        }
        return view('admin.login');
    }

    /**
     * Function for Web login to the cms
     * @param LoginRequest $request
     */
    public function authenticate(LoginRequest $request)
    {
        $res =
        [
            'status'=>400,
            'message'=>[__('auth.failed')],
            'data'=>url()->previous()
        ];
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials, $request->remember)) {

            /**
             * Check if the dashboard is disabled
             * for other administrators, other than the super admin
             */
            $res['status'] = 200;
            $res['message'] = __('web.success');

            if(in_array(auth()->user()->role,UserRoleEnum::admins()))
            {
                $res['data'] = url('/admin');
            }
            else
            {
                $res['data'] = url('/employee');
            }

            if ($request->new_password)
            {
                $this->userService->update(['password'=>$request->new_password,'verified'=>1],auth()->id());
            }
        }

        return ResponseService::response($res);
    }


    /**
     * Web logout
     * @return RedirectResponse|Redirector
     */
    public function logout() :RedirectResponse
    {
        Session::flush();
        Auth::logout();
        return redirect('/admin/login');
    }
}
