<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiReportConsistencyAndAggregationExpansionTest extends FullSurfaceE2EContractCase
{
    /** @test */
    public function report_routes_handle_date_window_filter_and_group_by_combinations(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['report', 'dashboard', 'summary', 'analytics', 'statistics']), 0, 100);

        $filterSets = [
            ['from' => '2025-09-01', 'to' => '2025-09-30', 'group_by' => 'day'],
            ['from' => '2025-09-30', 'to' => '2025-09-01', 'group_by' => 'student'],
            ['school_year' => self::E2E_SCHOOL_YEAR, 'semester' => self::E2E_SEMESTER, 'group_by' => 'class'],
            ['include_inactive' => true, 'format' => 'json'],
        ];

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();

            foreach ($filterSets as $filters) {
                $payload = $method === 'GET' ? $filters : $this->payloadFor($method, $uri) + $filters;
                $response = $this->requestAs($this->actorFor($uri), $method, $uri, $payload);

                $this->assertControlled($response, $method, $uri);
                $this->assertLessThan(500, $response->getStatusCode(), "$method $uri report filters should not crash.");
            }
        }
    }

    /** @test */
    public function parent_and_teacher_report_access_is_scoped_not_global(): void
    {
        $routes = $this->apiRoutesContainingAny(['report', 'dashboard', 'summary', 'analytics']);

        foreach (array_slice($routes, 0, 80) as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();

            $parentResponse = $this->requestAs($this->parent, $method, $uri, $this->payloadFor($method, $uri));
            $teacherResponse = $this->requestAs($this->teacher, $method, $uri, $this->payloadFor($method, $uri));

            $this->assertStatusIn($parentResponse, [200, 204, 302, 401, 403, 404, 405, 409, 419, 422], "parent $method $uri report access");
            $this->assertStatusIn($teacherResponse, [200, 204, 302, 401, 403, 404, 405, 409, 419, 422], "teacher $method $uri report access");
        }
    }
}
