<?php

namespace App\Http\Controllers;

use App\Models\Course;
use App\Models\Enrollment;
use App\Models\Grade;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;

class GradeController extends Controller
{
    /**
     * Show the form to edit grades for a course.
     *
     * Todo refactor to prevent the number of queries to depend upon the number of records
     */
    public function edit(Course $course): View
    {
        $this->checkAccessForCourse($course);

        return view('grades.edit', [
            'enrollments' => $course->enrollments,
            'course_grade_types' => $course->gradeTypes->sortBy('id') ?? collect(),
            'grades' => $course->grades,
            'course' => $course,
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'grade_type_id' => 'required|numeric',
            'enrollment_id' => 'required|numeric',
            'value' => 'required|numeric|min:0',
        ]);

        $enrollment = Enrollment::findOrFail($request->input('enrollment_id'));
        $this->checkAccessForCourse($enrollment->course);
        $grade = Grade::firstOrNew([
            'grade_type_id' => $request->input('grade_type_id'),
            'enrollment_id' => $request->input('enrollment_id'),
        ]);
        $grade->grade = $request->input('value');
        $grade->save();
    }

    public function getEnrollmentTotal(Request $request)
    {
        $enrollment = Enrollment::findOrFail($request->input('enrollment_id'));
        $this->checkAccessForCourse($enrollment->course);

        return $enrollment->grades->sum('grade');
    }

    protected function checkAccessForCourse(Course $course): void
    {
        if (Gate::forUser(backpack_user())->denies('edit-course-grades', $course)) {
            abort(403);
        }
    }
}
