<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch11RouteRegressionNetTest extends FullSurfaceE2EContractCase
{
    public function test_every_api_family_has_at_least_one_negative_contract_probe_in_batch_11(): void
    {
        $families = [
            'auth' => ['auth', 'login', 'logout', 'frontend'],
            'school_year' => ['school-year', 'school_year', 'semester'],
            'academic' => ['scores', 'grading', 'homework', 'quiz', 'report-card'],
            'attendance' => ['attendance', 'absence', 'late', 'dismissal'],
            'finance' => ['finance', 'invoice', 'payment', 'refund'],
            'inventory' => ['inventory', 'supplier', 'supply'],
            'communications' => ['messages', 'support', 'email', 'whatsapp', 'notification'],
            'family' => ['parent', 'family', 'guardian', 'authorized'],
            'admin' => ['admin', 'users', 'roles', 'permissions', 'settings'],
            'files' => ['import', 'export', 'download', 'print', 'certificate', 'badge'],
        ];

        foreach ($families as $family => $needles) {
            $this->assertNotEmpty(
                $this->apiRoutesContainingAny($needles),
                'Batch 11 expects at least one route for the ' . $family . ' family. If this fails, update the route catalog instead of pretending the family vanished.'
            );
        }
    }

    public function test_batch_11_high_risk_routes_return_controlled_responses_under_combined_hostile_input(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['auth', 'attendance', 'finance', 'students', 'users', 'settings', 'export', 'import', 'webhook', 'messages']), 0, 160);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $payload = $this->payloadFor($method, $uri) + [
                'id' => '../../../etc/passwd',
                'student_id' => ['bad' => true],
                'amount' => 'Infinity',
                'notes' => '<script>alert(1)</script>',
                'url' => 'http://169.254.169.254/latest/meta-data/',
                'return_url' => 'https://evil.example',
            ];

            $this->actingAs($this->actorFor($uri), 'api');
            $response = $this->withHeaders([
                'Accept' => 'application/json',
                'X-Forwarded-For' => '127.0.0.1',
                'Idempotency-Key' => 'batch-11-regression-net',
            ])->json($method, $this->materializePath($uri), $payload);

            $this->assertControlled($response, $method, $uri);
            $this->assertNoServerError($response, 'batch 11 combined hostile probe at ' . $method . ' ' . $uri);
        }
    }
}
