<?php

namespace Tests\Feature\Api\V1\FullSurface;

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


class ApiBatch20LegalHoldRetentionAndPurgeConflictContractTest extends FullSurfaceE2EContractCase
{
    public function test_batch20_legal_hold_retention_and_purge_conflict_stays_controlled(): void
    {
        $routes = $this->apiRoutesContainingAny(['privacy', 'retention', 'erase', 'purge', 'archive', 'delete', 'destroy', 'students', 'parents', 'family', 'files', 'documents']);
        $payload = [
            'legal_hold' => false,
            'force_purge' => true,
            'erase_children' => true,
            'include_archived' => true,
            'retention_policy' => 'client-forced-delete-now',
            'requested_by' => 999999,
            'reason' => 'client forces purge despite legal hold',
            'delete_files' => true,
        ];

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

            if (str_contains($uri, 'admin') || str_contains($uri, 'audit') || str_contains($uri, 'logs') || str_contains($uri, 'settings') || str_contains($uri, 'metrics') || str_contains($uri, 'health')) {
                $actor = $this->studentUser;
            }

            $response = $this->requestAs($actor, $method, $uri, $this->payloadFor($method, $uri) + $payload);
            $this->assertControlled($response, $method, $uri);
            $this->assertNotEquals(500, $response->getStatusCode(), $method.' '.$uri.' crashed during Batch 20 legal hold retention and purge conflict boundaries probe.');

            $body = strtolower($response->getContent());
            foreach (['sqlstate', 'stack trace', 'app_key', 'db_password', 'remember_token', 'api_token', 'password_hash', 'massassignmentexception', 'foreign key constraint', 'database.sqlite', '169.254.169.254'] as $leak) {
                $this->assertStringNotContainsString($leak, $body, $method.' '.$uri.' leaked '.$leak.' during Batch 20 legal hold retention and purge conflict boundaries probe.');
            }
        }
    }
}
