<?php

namespace App\Controllers;

use Wibiesana\Padi\Core\Controller;
use Wibiesana\Padi\Core\Query;
use App\Models\ClassroomMember;
use App\Resources\ClassroomMemberResource;

class StudentClassController extends Controller
{
    /**
     * Get students by class ID
     * GET /student-class/class/{classId}
     */
    public function getByClass()
    {
        $classId = $this->request->param('classId');

        if (!$classId || $classId === 'undefined') {
            return $this->response->json([
                'success' => true,
                'item' => [],
                'message' => 'Invalid class ID provided'
            ]);
        }

        $model = new ClassroomMember();
        $model->with(['class', 'student']);
        $query = Query::find()
            ->select("classroom_member.*")
            ->from("classroom_member")
            ->where(['class_id' => $classId]);

        $results = $query->all();

        if (!empty($results)) {
            $model->loadRelations($results);
        }

        return $this->response->json([
            'success' => true,
            'item' => ClassroomMemberResource::collection($results)
        ]);
    }
}
