<?php
namespace App\Http\Controllers;
use App\Models\Assessment;
use App\Models\AssessmentResult;
use App\Models\Enrollment;
use App\Models\TrainingClass;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class AssessmentController extends Controller {
    public function index(): View {
        $assessments=Assessment::with('trainingClass.course')->latest()->get();
        return view('assessments.index',compact('assessments'));
    }
    public function create(): View {
        $classes=TrainingClass::with('course')->latest()->get();
        return view('assessments.create',compact('classes'));
    }
    public function store(Request $request): RedirectResponse {
        $request->validate([
            'training_class_id'=>'required|exists:training_classes,id',
            'title'=>'required|max:255',
            'assessment_date'=>'nullable|date',
            'max_score'=>'required|numeric|min:0',
            'pass_score'=>'required|numeric|min:0',
        ]);
        Assessment::create($request->only(['training_class_id','title','type','assessment_date','max_score','pass_score','status','note']));
        return redirect()->route('assessments.index')->with('success','Đã thêm bài kiểm tra/đánh giá thành công.');
    }
    public function show(Assessment $assessment): View {
        $assessment->load('trainingClass.course');
        $enrollments=Enrollment::with('student')->where('training_class_id',$assessment->training_class_id)->get();
        $results=AssessmentResult::where('assessment_id',$assessment->id)->get()->keyBy('enrollment_id');
        return view('assessments.show',compact('assessment','enrollments','results'));
    }
    public function saveResults(Request $request, Assessment $assessment): RedirectResponse {
        foreach($request->input('score',[]) as $enrollmentId=>$score){
            if($score==='' || $score===null){ continue; }
            $result=((float)$score >= (float)$assessment->pass_score) ? 'Đạt' : 'Không đạt';
            AssessmentResult::updateOrCreate(
                ['assessment_id'=>$assessment->id,'enrollment_id'=>$enrollmentId],
                ['score'=>$score,'result'=>$result,'note'=>$request->input("note.$enrollmentId")]
            );
        }
        return redirect()->route('assessments.show',$assessment->id)->with('success','Đã lưu kết quả đánh giá thành công.');
    }
    public function edit(Assessment $assessment): View {
        $classes=TrainingClass::with('course')->latest()->get();
        return view('assessments.edit',compact('assessment','classes'));
    }
    public function update(Request $request, Assessment $assessment): RedirectResponse {
        $request->validate([
            'training_class_id'=>'required|exists:training_classes,id',
            'title'=>'required|max:255',
            'assessment_date'=>'nullable|date',
            'max_score'=>'required|numeric|min:0',
            'pass_score'=>'required|numeric|min:0',
        ]);
        $assessment->update($request->only(['training_class_id','title','type','assessment_date','max_score','pass_score','status','note']));
        return redirect()->route('assessments.index')->with('success','Đã cập nhật bài kiểm tra/đánh giá thành công.');
    }
    public function destroy(Assessment $assessment): RedirectResponse {
        $assessment->delete();
        return redirect()->route('assessments.index')->with('success','Đã xóa bài kiểm tra/đánh giá thành công.');
    }
}
