<?php

app_require('classes/error_report_schema_bootstrap.php');
app_require('classes/error_report_service.php');
app_require('classes/error_report_store.php');

use classes\error_report_service;

it('redacts sensitive error report payload fields recursively', function (): void {
    $payload = [
        'Authorization' => 'Bearer secret-token',
        'request' => [
            'headers' => [
                'X-Api-Key' => 'hidden',
                'Accept' => 'application/json',
            ],
            'body' => [
                'password' => 'not stored',
                'safe' => 'visible',
            ],
        ],
    ];

    expect(error_report_service::redactPayload($payload))->toBe([
        'Authorization' => '[redacted]',
        'request' => [
            'headers' => [
                'X-Api-Key' => '[redacted]',
                'Accept' => 'application/json',
            ],
            'body' => [
                'password' => '[redacted]',
                'safe' => 'visible',
            ],
        ],
    ]);
});

it('validates supported screenshot data uris', function (): void {
    $decoded = error_report_service::decodeScreenshotDataUri('data:image/png;base64,' . base64_encode('png-bytes'));

    expect($decoded['mime_type'])->toBe('image/png');
    expect($decoded['contents'])->toBe('png-bytes');
    expect($decoded['size_bytes'])->toBe(strlen('png-bytes'));

    expect(fn() => error_report_service::decodeScreenshotDataUri('data:text/plain;base64,' . base64_encode('nope')))
        ->toThrow(RuntimeException::class, 'Screenshot must be a PNG, JPEG, or WebP data URI.');
});

it('defines error report schema, routes, permissions, storage, and OpenAPI docs', function (): void {
    $schema = file_get_contents(app_path('classes/error_report_schema_bootstrap.php'));
    $service = file_get_contents(app_path('classes/error_report_service.php'));
    $store = file_get_contents(app_path('classes/error_report_store.php'));
    $route = file_get_contents(app_path('routes/errorReportRoute.php'));
    $openapi = file_get_contents(app_path('openapi.yaml'));

    expect($schema)->toContain('CREATE TABLE IF NOT EXISTS error_reports');
    expect($schema)->toContain('request_errors_json');
    expect($schema)->toContain('vue_errors_json');
    expect($schema)->toContain('data_collection_accepted_at');
    expect($schema)->toContain('resolved_by_user_id');

    expect($service)->toContain('createFromCurrentPrincipal');
    expect($service)->toContain('decodeScreenshotDataUri');
    expect($service)->toContain('data_collection_accepted');
    expect($service)->toContain('request_error_count');
    expect($service)->toContain('vue_error_count');
    expect($store)->toContain("error-reports/%s/%s.%s");

    expect($route)->toContain('/error-reports');
    expect($route)->toContain('/superuser/error-reports');
    expect($route)->toContain('/superuser/error-reports/{id}/status');
    expect($route)->toContain("requirePermission('superuser_error_reports_view')");
    expect($route)->toContain("requirePermission('superuser_error_reports_resolve')");

    expect($openapi)->toContain('/error-reports:');
    expect($openapi)->toContain('ErrorReportSubmissionRequest');
    expect($openapi)->toContain('ErrorReportStatusUpdateRequest');
    expect($openapi)->not->toContain("        - screenshot\n");
    expect($openapi)->toContain('Reports are accepted without an attachment when capture or upload fails.');
});
