<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch13FullSurfaceRegressionSuperSweepTest extends FullSurfaceE2EContractCase
{
    public function test_batch13_super_sweep_keeps_major_route_families_under_controlled_failure(): void
    {
        $families = [
            'identity' => ['auth', 'login', 'profile', 'users', 'roles', 'permissions'],
            'school' => ['students', 'parents', 'families', 'classes', 'school-year', 'semester'],
            'academic' => ['teacher', 'scores', 'homework', 'quiz', 'report-card', 'certificate'],
            'safety' => ['attendance', 'dismissal', 'late', 'incident', 'emergency'],
            'finance' => ['finance', 'invoice', 'payment', 'refund', 'fee', 'discount'],
            'operations' => ['inventory', 'supplier', 'procurement', 'settings', 'configuration'],
            'communications' => ['message', 'support', 'email', 'whatsapp', 'notification'],
            'files' => ['import', 'export', 'download', 'print', 'upload', 'media'],
        ];

        foreach ($families as $needles) {
            foreach (array_slice($this->apiRoutesContainingAny($needles), 0, 25) as $route) {
                $method = $this->primaryMethod($route);
                $payload = $this->payloadFor($method, $route->uri()) + [
                    'id' => '../../etc/passwd',
                    'student_id' => 'not-an-int',
                    'parent_id' => ['array-is-not-id'],
                    'amount' => 'NaN',
                    'status' => '<script>bad()</script>',
                    'include' => '*',
                    'force' => true,
                ];

                $response = $this->requestAs($this->actorFor($route->uri()), $method, $route->uri(), $payload);
                $this->assertControlled($response, $method, $route->uri());

                $body = strtolower($response->getContent());
                $this->assertStringNotContainsString('sqlstate', $body);
                $this->assertStringNotContainsString('app_key', $body);
                $this->assertStringNotContainsString('stack trace', $body);
            }
        }
    }

    public function test_batch13_major_mutation_families_are_represented(): void
    {
        $expectedFamilies = [
            'students', 'attendance', 'scores', 'finance', 'payment', 'inventory',
            'message', 'support', 'settings', 'roles', 'permissions', 'family',
        ];

        foreach ($expectedFamilies as $family) {
            $mutatingRoutes = array_filter($this->apiRoutesContainingAny([$family]), function ($route): bool {
                return in_array($this->primaryMethod($route), ['POST', 'PUT', 'PATCH', 'DELETE'], true);
            });

            $this->assertNotEmpty($mutatingRoutes, "Batch 13 expected at least one mutating API route for {$family}.");
        }
    }
}
