<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\CompanyProfile;
use App\Models\MasterDealer;
use App\Models\SubDealer;
use App\Models\TrialCompanyProfile;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Str;

class ResetPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */

    use ResetsPasswords;

    /**
     * Where to redirect users after resetting their password.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    public function showAdminResetForm()
    {
        return view('auth.admin.auth-forgot-password');
    }

    public function showMasterDealerResetForm()
    {
        return view('auth.master-dealer.auth-forgot-password');
    }

    public function showSubDealerResetForm()
    {
        return view('auth.sub-dealer.auth-forgot-password');
    }

    public function showPaidAccountResetForm()
    {
        return view('auth.paid-account.auth-forgot-password');
    }

    public function showTrialAccountResetForm()
    {
        return view('auth.trial-account.auth-forgot-password');
    }


    public function resetPasswordByEmail(Request $request)
    {

        $request->validate([
            'email' => 'required|email',
            'role' => 'required|string'
        ]);


        $email = $request->only('email');
        $role = $request->input('role');


        $status = null;

        switch ($role) {
            case 'admin':
                $status = Password::broker('admins')
                    ->sendResetLink($email, function (Admin $admin, string $token) {
                        $admin->sendPasswordResetNotification($token);
                    });
                break;
            case 'master-dealer':
                $status = Password::broker('master-dealers')
                    ->sendResetLink($email, function (MasterDealer $masterDealer, string $token) {
                        $masterDealer->sendPasswordResetNotification($token);
                    });
                break;
            case 'sub-dealer':
                $status = Password::broker('sub-dealers')
                    ->sendResetLink($email, function (SubDealer $subDealer, string $token) {
                        $subDealer->sendPasswordResetNotification($token);
                    });
                break;
            case 'paid-account':
                $status = Password::broker('paid-accounts')
                    ->sendResetLink($email, function (CompanyProfile $companyProfile, string $token) {
                        $companyProfile->sendPasswordResetNotification($token);
                    });
                break;
            case 'trial-account':
                $status = Password::broker('trial-accounts')
                    ->sendResetLink($email, function (TrialCompanyProfile $trialCompanyProfile, string $token) {
                        $trialCompanyProfile->sendPasswordResetNotification($token);
                    });
                break;
        }

        return $status === Password::RESET_LINK_SENT
            ? back()->with(['status' => __($status)])
            : back()->withErrors(['email' => __($status)]);

    }


    public function resetPasswordView(string $token, Request $request)
    {
        $email = $request->input('email');
        return view('auth.auth-reset-password', compact('token', 'email'));
    }

    public function validateResetPassword(Request $request)
    {
        $request->validate([
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:8|confirmed',
        ]);


        //Split the role from the token
        $data = explode('-', $request->input('token'));
        $token = $data[0];
        $role = $data[1];


        $email = $request->input('email');
        $password = $request->input('password');
        $passwordConfirmation = $request->input('password_confirmation');

        $status = '';

        $temp = [

            'email' => $email,
            'password' => $password,
            'password_confirmation' => $passwordConfirmation,
            'token' => $token,
        ];


        switch ($role) {
            case '01' :
                {
                    $status = Password::broker('admins')
                        ->reset(
                            $temp,
                            function (Admin $user, string $password) {
                                $user->forceFill([
                                    'password' => Hash::make($password)
                                ])->setRememberToken(Str::random(60));
                                $user->save();
                                event(new PasswordReset($user));
                            }
                        );
                }
                break;

            case '02' :
                {
                    $status = Password::broker('master-dealers')
                        ->reset(
                            $temp,
                            function (MasterDealer $user, string $password) {
                                $user->forceFill([
                                    'password' => Hash::make($password)
                                ]);
                                $user->save();
                                event(new PasswordReset($user));
                            }
                        );
                }
                break;

            case '03' :
                {
                    $status = Password::broker('sub-dealers')
                        ->reset(
                            $temp,
                            function (SubDealer $user, string $password) {
                                $user->forceFill([
                                    'password' => Hash::make($password)
                                ]);
                                $user->save();
                                event(new PasswordReset($user));
                            }
                        );
                }
                break;

            case '04' :
                {
                    $status = Password::broker('paid-accounts')
                        ->reset(
                            $temp,
                            function (CompanyProfile $user, string $password) {
                                $user->forceFill([
                                    'raw_password' => $password,
                                    'password' => Hash::make($password)
                                ]);
                                $user->save();
                                event(new PasswordReset($user));
                            }
                        );
                }
                break;

            case '05' :
                {
                    $status = Password::broker('trial-accounts')
                        ->reset(
                            $temp,
                            function (TrialCompanyProfile $user, string $password) {
                                $user->forceFill([
                                    'raw_password' => $password,
                                    'password' => Hash::make($password)
                                ]);
                                $user->save();
                                event(new PasswordReset($user));
                            }
                        );
                }
                break;


        }

        if ($status === Password::PASSWORD_RESET) {
            switch ($role) {
                case '01' :
                {
                    return redirect()->route('admin.login-view')->with('status', __($status));
                }


                case '02' :
                {
                    return redirect()->route('master-dealer.login-view')->with('status', __($status));

                }


                case '03' :
                {
                    return redirect()->route('sub-dealer.login-view')->with('status', __($status));

                }


                case '04' :
                {
                    return redirect()->route('paid-account.login-view')->with('status', __($status));

                }


                case '05' :
                {
                    return redirect()->route('trial-account.login-view')->with('status', __($status));

                }

                default:
                {
                    return back()->withErrors(['email' => [__($status)]]);
                }

            }
        } else {
            return back()->withErrors(['email' => [__($status)]]);
        }


//        return $status === Password::PASSWORD_RESET
//            ? redirect()->route('login')->with('status', __($status))
//            : back()->withErrors(['email' => [__($status)]]);

    }
}
