<?php

namespace App\Http\Controllers\Api\Administrator;

use App\Http\Controllers\Api\Core\BaseApiController;
use App\Http\Requests\Administrator\TrophyReportRequest;
use App\Services\Administrator\Trophy\TrophyReportService;
use Illuminate\Http\JsonResponse;

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

    public function index(TrophyReportRequest $request): JsonResponse
    {
        $payload = $request->validated();

        return response()->json([
            'ok' => true,
            'data' => $this->service->projection(
                $payload['school_year'] ?? null,
                isset($payload['percentile']) ? (float) $payload['percentile'] : null
            ),
        ]);
    }

    public function winners(TrophyReportRequest $request): JsonResponse
    {
        $payload = $request->validated();

        return response()->json([
            'ok' => true,
            'data' => $this->service->winners(
                $payload['school_year'] ?? null,
                isset($payload['percentile']) ? (float) $payload['percentile'] : null
            ),
        ]);
    }

    public function final(TrophyReportRequest $request): JsonResponse
    {
        $payload = $request->validated();

        return response()->json([
            'ok' => true,
            'data' => $this->service->final(
                $payload['school_year'] ?? null,
                isset($payload['percentile']) ? (float) $payload['percentile'] : null
            ),
        ]);
    }
}
