<?php

namespace App\Http\Controllers\Api\Scores;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Scores\ScorePredictorRequest;
use App\Http\Resources\Scores\ScorePredictorStudentResource;
use App\Services\Scores\ScorePredictorService;
use Illuminate\Http\JsonResponse;

class ScorePredictorController extends BaseApiController
{
    public function __construct(private ScorePredictorService $service)
    {
        parent::__construct();
    }

    public function index(ScorePredictorRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $data = $this->service->report(
            $payload['school_year'] ?? null,
            isset($payload['class_section_id']) ? (int) $payload['class_section_id'] : null
        );

        return response()->json([
            'ok' => true,
            'students' => ScorePredictorStudentResource::collection($data['students'] ?? []),
            'school_year' => $data['school_year'] ?? null,
            'class_sections' => $data['class_sections'] ?? [],
            'selected_class_section_id' => $data['selected_class_section_id'] ?? null,
            'semester' => $data['semester'] ?? null,
        ]);
    }
}
