<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiCrossModuleReferentialWorkflowExpansionTest extends FullSurfaceE2EContractCase
{
    /** @test */
    public function wrong_but_existing_ids_across_modules_do_not_silently_cross_link_records(): void
    {
        $routes = array_slice(array_filter($this->apiRoutes(), fn ($route) => in_array($this->primaryMethod($route), ['POST', 'PUT', 'PATCH'], true)), 0, 120);

        $crossLinked = [
            'student_id' => $this->ids['invoiceId'],
            'invoice_id' => $this->ids['studentId'],
            'parent_id' => $this->teacher->id,
            'teacher_id' => $this->parent->id,
            'class_section_id' => $this->ids['parentId'],
            'user_id' => $this->ids['studentId'],
        ];

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $response = $this->requestAs($this->actorFor($uri), $method, $uri, $this->payloadFor($method, $uri) + $crossLinked);

            $this->assertControlled($response, $method, $uri);
            $this->assertLessThan(500, $response->getStatusCode(), "$method $uri should not explode on cross-module ID misuse.");
        }
    }
}
