<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch12AttendanceCorrectionAuditContractTest extends FullSurfaceE2EContractCase
{
    public function test_attendance_corrections_require_valid_actor_date_status_and_reason(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['attendance', 'absence', 'late', 'dismissal', 'slip']), 0, 150);
        $payloads = [
            ['status' => 'present', 'reason' => ''],
            ['status' => 'teleported', 'reason' => 'nonsense'],
            ['date' => '1900-01-01', 'reason' => 'too old'],
            ['date' => '2099-12-31', 'reason' => 'future'],
            ['student_id' => $this->ids['invoiceId'], 'reason' => 'wrong id type'],
        ];

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

            foreach ($payloads as $payload) {
                $response = $this->requestAs($this->teacher, $method, $route->uri(), $this->payloadFor($method, $route->uri()) + $payload);
                $this->assertControlled($response, $method, $route->uri());
                $this->assertNoServerError($response, 'attendance correction audit '.$method.' '.$route->uri());
            }
        }
    }

    public function test_parent_and_student_cannot_correct_attendance_records(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['attendance', 'absence', 'late', 'dismissal']), 0, 100);

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

                $response = $this->requestAs($actor, $method, $route->uri(), $this->payloadFor($method, $route->uri()));
                $this->assertStatusIn($response, [401, 403, 404, 405, 409, 419, 422], 'low privilege attendance correction '.$method.' '.$route->uri());
                $this->assertNoServerError($response, 'low privilege attendance correction '.$method.' '.$route->uri());
            }
        }
    }
}
