<?php

declare(strict_types=1);

namespace Kylin\Education\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Kylin\Education\Models\Classroom;

/**
 * 班级控制器
 */
class ClassroomController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $classrooms = Classroom::paginate($request->input('per_page', 15));

        return response()->json(['code' => 0, 'data' => $classrooms]);
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:100',
            'capacity' => 'required|integer|min:1',
            'equipment' => 'nullable|string|max:500',
            'available_slots' => 'nullable|array',
        ]);

        $classroom = Classroom::create($validated);

        return response()->json(['code' => 0, 'data' => $classroom], 201);
    }

    public function show(Classroom $classroom): JsonResponse
    {
        $classroom->load('schedules.course');

        return response()->json(['code' => 0, 'data' => $classroom]);
    }

    public function update(Request $request, Classroom $classroom): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'sometimes|string|max:100',
            'capacity' => 'sometimes|integer|min:1',
            'equipment' => 'nullable|string|max:500',
            'available_slots' => 'nullable|array',
        ]);

        $classroom->update($validated);

        return response()->json(['code' => 0, 'data' => $classroom]);
    }

    public function destroy(Classroom $classroom): JsonResponse
    {
        $classroom->delete();

        return response()->json(['code' => 0, 'message' => '删除成功']);
    }
}
