<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Imports\BulkMarkImport;
use App\Models\Mark;
use App\Models\User;
use Auth;
use Excel;

class MarkController extends Controller
{
    public function view()
    {
        $mark_details = Mark::latest()->get();
        if(count($mark_details) > 0)
        {
            return response()->json(['result' => true, 'mark_details' => $mark_details]);
        }
        else
        {
            return response()->json(['result' => false, 'message' => 'No Marks Found']);
        }
    }
    
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'mark_id' => 'required',
            'student_id' => 'required',
            'teacher_id' => 'required',
            'exam_type' => 'required',
        ]);
        
        if($validator->fails()){
            return response()->json(['result' => false, 'message' => $validator->errors()]);    
        }
        
        $mark                = Mark::find($request->mark_id);
        $mark->student_id    = $request->student_id;
        $mark->teacher_id    = $request->teacher_id;
        $mark->exam_type     = $request->exam_type;
        $mark->paper_1       = $request->paper_1;
        $mark->paper_2       = $request->paper_2;
        $mark->paper_3       = $request->paper_3;
        $mark->paper_4       = $request->paper_4;
        $mark->paper_5       = $request->paper_5;
        $mark->grade         = $request->grade;
        
        if ($mark->save()) {
            return response()->json(['result' => true, 'message' => 'Mark Details Updated successfully']);
        }
        else{
            return response()->json(['result' => false, 'message' => 'Somthing Wrong']);
        }
    }
    
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'student_id' => 'required',
            'teacher_id' => 'required',
            'exam_type' => 'required',
        ]);
        
        if($validator->fails()){
            return response()->json(['result' => false, 'message' => $validator->errors()]);    
        }
        
        $mark                = new Mark();
        $mark->student_id    = $request->student_id;
        $mark->teacher_id    = $request->teacher_id;
        $mark->exam_type     = $request->exam_type;
        $mark->paper_1       = $request->paper_1;
        $mark->paper_2       = $request->paper_2;
        $mark->paper_3       = $request->paper_3;
        $mark->paper_4       = $request->paper_4;
        $mark->paper_5       = $request->paper_5;
        $mark->grade         = $request->grade;
        
        if ($mark->save()) {
            return response()->json(['result' => true, 'message' => 'Mark Details store successfully']);
        }
        else{
            return response()->json(['result' => false, 'message' => 'Somthing Wrong']);
        }
    }
    
    public function edit(Request $request)
    {
        $mark = Mark::where('id', $request->mark_id)->first();
        if($mark)
        {
            return response()->json(['result' => true, 'mark' => $mark]);
        }
        
        return response()->json(['result' => false, 'message' => 'Mark not Found']);
    }
    
    public function delete(Request $request)
    {
        $mark = Mark::where('id', $request->mark_id)->first();
        if($mark)
        {
            Mark::findOrFail($request->mark_id)->delete();
            return response()->json(['result' => true, 'message' => 'Mark Deleted Successfully']);
        }
        
        return response()->json(['result' => false, 'message' => 'Mark not Found']);

    }
    
    public function student()
    {
        $student = User::where('user_type', 'student')->latest()->get();
        if(count($student) > 0)
        {
            return response()->json(['result' => true, 'student' => $student]);
        }
        else
        {
            return response()->json(['result' => false, 'message' => 'No Student Found']);
        }
    }
    
    public function BulkUpload(Request $request)
    {
        
        // dd($request->all());
        
        $validator = Validator::make($request->all(), [
            'upload_file'  => 'required|mimes:xls,xlsx'
        ]);
        
        $path = $request->file('upload_file');

        $data['teacher_id'] = $request->teacher_id;

        $check_data = Excel::import(new BulkMarkImport($data), $path);

        if ($check_data) {
            return response()->json(['result' => true, 'message' => 'Bulk Mark Import Successfully']);
        }
        else
        {
            return response()->json(['result' => false, 'message' => 'Something Wrong']);
        }
        
    }
}
