<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Grade;
use App\Http\Requests\StoreGradeRequest;

/**
 * @OA\Post(
 *     path="/grades",
 *     summary="Registrar nota",
 *     tags={"Grades"},
 *     security={{"bearerAuth":{}}},
 *     @OA\RequestBody(
 *         required=true,
 *         @OA\JsonContent(
 *             required={"student_id","score"},
 *             @OA\Property(property="student_id", type="integer", example=1),
 *             @OA\Property(property="score", type="number", example=18)
 *         )
 *     ),
 *     @OA\Response(
 *         response=201,
 *         description="Nota registrada"
 *     ),
 *     @OA\Response(
 *         response=401,
 *         description="No autorizado"
 *     )
 * )
 */

class GradeController extends Controller
{
    public function index()
    {
        return Grade::with('student','user')->get();
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'student_id' => 'required|exists:students,id',
            'user_id' => 'required|exists:users,id',
            'score' => 'required|numeric|min:0|max:20'
        ]);

        return response()->json(Grade::create($data), 201);
    }

    public function show(Grade $grade)
    {
        return $grade->load('student','user');
    }

    public function update(Request $request, Grade $grade)
    {
        $grade->update($request->all());
        return response()->json($grade);
    }

    public function destroy(Grade $grade)
    {
        $grade->delete();
        return response()->json(['message' => 'Nota eliminada']);
    }
}
