<?php

namespace App\Http\Controllers;

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

use Yajra\DataTables\Facades\DataTables;

use App\Models\Notice;
use App\Models\Student;

class NoticeController extends Controller
{
  public function index(Request $request)
  {
    Session::forget('status');

    try {
      return view('notices.index', compact('request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list notice failed';
      }
    }
  }

  public function dtGetNotices()
  {
    try {
      $notices = Notice::select(['id', 'date', 'subject', 'content']);

      return DataTables::eloquent($notices)->toJson();
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list notice failed';
      }
    }
  }

  public function getStudentUnreadNoticesCount(
    Request $request,
    string $student_id
  ) {
    $already_read_notices = DB::select(
      'select notice_id from `notice_student_rel` where student_id = ?',
      [$student_id]
    );

    $already_read_ids = [];
    foreach ($already_read_notices as $already_read_notice) {
      array_push($already_read_ids, $already_read_notice->notice_id);
    }

    $all_unread_notices = Notice::select(['id', 'date', 'subject', 'content'])
      ->whereNotIn('id', $already_read_ids)
      ->count();

    return json_encode(['unread' => $all_unread_notices]);
  }

  public function dtGetStudentUnreadNotices(
    Request $request,
    string $student_id
  ) {
    try {
      $current_student = Student::find($student_id);
      $student_all_notice = Notice::where(
        'to',
        '=',
        $current_student->email
      )->get();

      $already_read_notices = DB::select(
        'select notice_id from `notice_student_rel` where student_id = ?',
        [$student_id]
      );

      $already_read_ids = [];
      foreach ($already_read_notices as $already_read_notice) {
        array_push($already_read_ids, $already_read_notice->notice_id);
      }

      $all_unread_notices = Notice::select(['id', 'date', 'subject', 'content'])
        ->where('to', '=', $current_student->email)
        ->whereNotIn('id', $already_read_ids);

      return DataTables::eloquent($all_unread_notices)->toJson();
    } catch (\Exception $e) {
      dd($e);
      $debug = config('app.debug');

      if ($debug) {
        dd($e);
      } else {
        return 'sorry but list notice failed';
      }
    }
  }

  public function show(Notice $notice, Request $request)
  {
    try {
      return view('notices.show', compact('notice', 'request'));
    } catch (\Exception $e) {
      dd($e);
      return 'show notice failed';
    }
  }

  public function edit(Notice $notice, Request $request)
  {
    try {
      return view('notices.edit', compact('notice', 'request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');
      if ($debug) {
        dd($e);
      } else {
        return 'sorry but edit notice failed';
      }
    }
  }

  public function destroy(Notice $notice)
  {
    try {
      $notice->delete();

      return redirect()
        ->route('notices.index')
        ->with('success', 'notice has been deleted successfully');
    } catch (\Exception $e) {
      $debug = config('app.debug');

      if ($debug) {
        dd($e);
      } else {
        return 'create notices failed';
      }
    }
  }

  public function create(Request $request)
  {
    try {
      return view('notices.create', compact('request'));
    } catch (\Exception $e) {
      $debug = config('app.debug');

      if ($debug) {
        dd($e);
      } else {
        return 'create notices failed';
      }
    }
  }
}
