<?php

namespace App\Http\Controllers\Admin;

use App\Events\NewNote;
use App\Models\Student;
use App\Models\StudentNote;
use Illuminate\Http\Request;
use App\DataTables\NotesDataTable;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class NotesController extends Controller
{
    //
    // public function notes()
    // {
    //     return view('admin.notes.notes-index');
    // }

    public function notes(NotesDataTable $notesDataTable)
    {
        return $notesDataTable->render('admin.notes.notes-index');
    }



    public function create(Request $request)
    {
        $data = ($request->all());
        $validatedData = Validator::make($data, [
            'student_id' => ['required', 'max:255', 'exists:App\Models\Student,id'],
            'text' => ['required'],
            'type' => ['required', 'max:255', 'exists:student_notes_types,id'],

            'studentIds' => 'required|array|min:1',
            'studentIds.*' => 'required|integer|exists:App\Models\Student,id'
        ], [], [])->validate();


        StudentNote::create([
            'student_id' => $validatedData['student_id'],
            'text' => $validatedData['text'],
            'type_id' => $validatedData['type'],
            'user_id' => Auth::guard('admin')->user()->id,
        ]);

        
        event(new NewNote($validatedData['student_id']));


        $data = [
            "code" => 1,
            "data" => [],
            "msg" => trans('general.record_added_successfully')
        ];

        return response($data, 200);
    }

    public function delete(Request $request)
    {
        $data = $request->all();
        Validator::make($data, [
            'note_id' => ['required', 'max:190', 'exists:App\Models\StudentNote,id'],
        ], [], [])->validate();

        $note = StudentNote::find($data['note_id']);
        $this->authorize('manage', $note);

        try {
            $note->delete();
            return back()->with('success', trans('general.record_deleted_successfully'));
        } catch (\Illuminate\Database\QueryException $th) {
            return back()->withErrors(trans('general.record_foreign_key_delete_error'));
        } catch (\Throwable $th) {
            return back()->withErrors(trans('general.record_delete_error'));
        }
    }

    public function notesForMultyStudents(Request $request){
        $notesTypes=DB::table('student_notes_types')->get();
        $students=Student::all();
        //dd($noteTypes);
        return view('admin.notes.notes-for-multy-students')->with(['notesTypes'=> $notesTypes,'students'=>$students]);
    }

    public function addNotesForMultyStudents(Request $request){
        $data = ($request->all());
        $validatedData = Validator::make($data, [
            'text' => ['required'],
            'type' => ['required', 'max:255', 'exists:student_notes_types,id'],
            'studentIds' => 'required|array|min:1',
            'studentIds.*' => 'required|integer|exists:App\Models\Student,id'
        ], [], [])->validate();

        foreach($validatedData['studentIds'] as $student){
            StudentNote::create([
                'student_id' => $student,
                'text' => $validatedData['text'],
                'type_id' => $validatedData['type'],
                'user_id' => Auth::guard('admin')->user()->id,
            ]);
            event(new NewNote($student));
        }
        
        $data = [
            "code" => 1,
            "data" => [],
            "msg" => trans('general.record_added_successfully')
        ];

        return back()->with($data['msg']);
    }
}
