<?php

namespace App\Http\Controllers;

use Str;
use Hash;

use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Password;

use App\Models\User;

class SessionsController extends Controller
{
  public function create()
  {
    try {
      // NOTE: switch to control text on login
      // e.g. debug@school.com / debug
      $show_login_hints = true;

      return view('sessions.create', compact('show_login_hints'));
    } catch (\Exception $e) {
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but create session failed';
      }
    }
  }

  public function store()
  {
    $attributes = request()->validate([
      'email' => 'required|email',
      'password' => 'required',
    ]);

    if (!auth()->attempt($attributes)) {
      throw ValidationException::withMessages([
        'email' => 'Your provided credentials could not be verified.',
      ]);
    }

    session()->regenerate();

    $email_to_find = $attributes['email'];
    $user = User::where('email', '=', $email_to_find)->get()[0];
    $role = $user->role;

    if ($role == 'debug') {
      return redirect('/dashboard');
      // ->route('studentShowDashboard')
      // ->with('success', 'update subject selection done');
    } elseif ($role == 'teacher') {
      return redirect()
        ->route('teacherShowDashboard')
        ->with('success', 'update subject selection done');
    } elseif ($role == 'student') {
      return redirect()
        ->route('studentShowDashboard')
        ->with('success', 'update subject selection done');
    } elseif ($role == 'admin') {
      return redirect('/dashboard');
      // ->route('showAdminDashboard')
      // ->with('success', 'update subject selection done');
    } else {
      return redirect('/dashboard');
      // ->route('studentShowDashboard')
      // ->with('success', 'update subject selection done');
    }
  }

  public function show()
  {
    try {
      request()->validate([
        'email' => 'required|email',
      ]);

      $status = Password::sendResetLink(request()->only('email'));

      return $status === Password::RESET_LINK_SENT
        ? back()->with(['status' => __($status)])
        : back()->withErrors(['email' => __($status)]);
    } catch (\Exception $e) {
      dd($e);
    }
  }

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

    $status = Password::reset(
      request()->only('email', 'password', 'password_confirmation', 'token'),
      function ($user, $password) {
        $user
          ->forceFill([
            'password' => $password,
          ])
          ->setRememberToken(Str::random(60));

        $user->save();

        event(new PasswordReset($user));
      }
    );

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

  public function destroy()
  {
    auth()->logout();

    return redirect('/sign-in');
  }
}
