<?php

namespace Tests\Feature\Api\V1\FullSurface;

use Tests\Feature\Api\V1\FullSurface\Support\FullSurfaceE2EContractCase;

class ApiBatch12AcademicGradeBoundaryContractTest extends FullSurfaceE2EContractCase
{
    public function test_grade_and_score_routes_reject_out_of_range_and_wrong_student_contexts(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['score', 'grade', 'homework', 'quiz', 'project', 'midterm', 'final', 'participation', 'report-card']), 0, 160);
        $scores = [-1, 101, 'A+++', 'NaN', 'Infinity', 999999];

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            if (! in_array($method, ['POST', 'PUT', 'PATCH'], true)) {
                continue;
            }

            foreach ($scores as $score) {
                $response = $this->requestAs($this->teacher, $method, $route->uri(), $this->payloadFor($method, $route->uri()) + [
                    'score' => $score,
                    'points' => $score,
                    'student_id' => $this->ids['invoiceId'],
                    'class_section_id' => $this->ids['invoiceId'],
                ]);

                $this->assertControlled($response, $method, $route->uri());
                $this->assertNoServerError($response, 'academic grade boundary '.$method.' '.$route->uri());
            }
        }
    }

    public function test_locked_or_finalized_grade_routes_do_not_accept_low_privilege_overrides(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['lock', 'unlock', 'finalize', 'publish', 'report-card']), 0, 100);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            if (! in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'], true)) {
                continue;
            }

            $response = $this->requestAs($this->parent, $method, $route->uri(), $this->payloadFor($method, $route->uri()) + ['force' => true, 'locked' => false]);
            $this->assertStatusIn($response, [401, 403, 404, 405, 409, 419, 422], 'parent grade finalization override '.$method.' '.$route->uri());
            $this->assertNoServerError($response, 'parent grade finalization override '.$method.' '.$route->uri());
        }
    }
}
