<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Student;
use App\Http\Requests\StoreStudentRequest;
use Illuminate\Http\JsonResponse;

/**
 * @OA\Get(
 *     path="/students",
 *     summary="Listar estudiantes",
 *     tags={"Students"},
 *     security={{"bearerAuth":{}}},
 *     @OA\Response(
 *         response=200,
 *         description="Listado de estudiantes"
 *     )
 * )
 */

class StudentController extends Controller
{
    public function index(): JsonResponse
    {
        return response()->json([
            'success' => true,
            'data' => Student::all()
        ]);
    }

    public function store(StoreStudentRequest $request): JsonResponse
    {
        $student = Student::create($request->validated());

        return response()->json([
            'success' => true,
            'data' => $student,
            'message' => 'Estudiante registrado correctamente'
        ], 201);
    }

    public function show($id): JsonResponse
    {
        $student = Student::find($id);

        if (!$student) {
            return response()->json([
                'success' => false,
                'message' => 'Estudiante no encontrado'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $student
        ]);
    }

    public function update(StoreStudentRequest $request, $id): JsonResponse
    {
        $student = Student::find($id);

        if (!$student) {
            return response()->json([
                'success' => false,
                'message' => 'Estudiante no encontrado'
            ], 404);
        }

        $student->update($request->validated());

        return response()->json([
            'success' => true,
            'data' => $student,
            'message' => 'Estudiante actualizado correctamente'
        ]);
    }

    public function destroy($id): JsonResponse
    {
        $student = Student::find($id);

        if (!$student) {
            return response()->json([
                'success' => false,
                'message' => 'Estudiante no encontrado'
            ], 404);
        }

        $student->delete();

        return response()->json([
            'success' => true,
            'message' => 'Estudiante eliminado correctamente'
        ], 204);
    }
}
