<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Student;
use App\Models\Teacher;
use App\Models\Admin;
use App\Models\User;

use App\Models\ClassRoom;
use App\Models\Subject;
use App\Models\Schedule;

class DashboardController extends Controller
{
  public function index(Request $request)
  {
    try {
      $num_of_student = Student::count();
      $num_of_teacher = Teacher::count();
      $num_of_classroom = ClassRoom::where('deleted', 'false')->count();
      $num_of_subject = Subject::where('deleted', 'false')->count();
      $num_of_schedule = Schedule::count();

      $user_role = $request->user()->role;
      $user_email = $request->user()->email;

      if ($user_role == 'student') {
        $student = Student::where('email', '=', $user_email)->get();
        $current_student = $student[0];
        $studentId = $current_student->id;
        return redirect()->route('studentShowDashboard');
      } elseif ($user_role == 'teacher') {
        $teacher = Teacher::where('email', '=', $user_email)->get();
        $current_teacher = $teacher[0];
        $teacherId = $current_teacher->id;
        return redirect()->route('teacherShowDashboard');
      } elseif ($user_role == 'admin') {
        $admin = Admin::where('email', '=', $user_email)->get();
        $current_admin = $admin[0];
        $adminId = $current_admin->id;

        return redirect()->route('showAdminDashboard');
      } elseif ($user_role == 'debug') {
        // NOTE: debug user fall here
        $user = User::where('email', '=', $user_email)->get();
        $current_user = $user[0];

        return view(
          'dashboard.index',
          compact(
            'request',
            'num_of_student',
            'num_of_teacher',
            'num_of_classroom',
            'num_of_subject',
            'num_of_schedule',
            'current_user'
          )
        );
      } else {
        // NOTE: should not see me
      }
    } catch (\Exception $e) {
      dd($e);
      header_remove();

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