<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Club;
use App\Models\Event;
use App\Models\Message;
use App\Models\SchoolClass;
use App\Models\TimetableSlot;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SchoolResourceController extends Controller
{
    public function classes(Request $request): JsonResponse
    {
        return response()->json(
            SchoolClass::query()->with('classTeacher')->orderBy('name')->paginate($request->integer('per_page', 25))
        );
    }

    public function clubs(Request $request): JsonResponse
    {
        return response()->json(
            Club::query()->withCount('students')->orderBy('name')->paginate($request->integer('per_page', 25))
        );
    }

    public function events(Request $request): JsonResponse
    {
        return response()->json(
            Event::query()->where('status', 'scheduled')->orderBy('starts_at')->paginate($request->integer('per_page', 25))
        );
    }

    public function messages(Request $request): JsonResponse
    {
        return response()->json(
            Message::query()->with('sender')->latest('sent_at')->paginate($request->integer('per_page', 25))
        );
    }

    public function timetable(Request $request): JsonResponse
    {
        return response()->json(
            TimetableSlot::query()->with(['schoolClass', 'subject', 'staff'])->orderBy('day_of_week')->paginate($request->integer('per_page', 50))
        );
    }
}
