<?php

declare(strict_types=1);

usesApiSuite();

function error_report_api_payload(array $overrides = []): array
{
    return array_replace_recursive([
        'before_error' => 'Opening the orders page',
        'expected' => 'The orders should load',
        'actual' => 'The page showed an error',
        'data_collection_accepted' => true,
        'data_collection_policy_version' => 'error-report-v1',
        'route_path' => '/admin/orders',
        'page_url' => 'https://app.example.test/admin/orders',
        'release_trace_id' => 'trace-error-report-test',
        'frontend_version' => 'frontend-test',
        'api_version' => 'api-test',
        'request_errors' => [
            ['method' => 'GET', 'url' => '/orders', 'statusCode' => 500],
        ],
        'vue_errors' => [
            ['type' => 'vue_component_error', 'payload' => ['message' => 'Render failed']],
        ],
        'context' => [
            'viewport' => ['width' => 1280, 'height' => 720],
            'user_agent' => 'ErrorReportsApiTest',
            'captured_at' => '2026-07-06T10:00:00.000Z',
            'data_collection_policy_version' => 'error-report-v1',
        ],
    ], $overrides);
}

function error_report_api_cleanup(array $report): void
{
    $id = (int)($report['id'] ?? 0);
    if ($id > 0) {
        api_fixtures()->cleanupDeleteById('error_reports', $id);
    }
}

it('creates error reports when screenshot capture failed', function (): void {
    api_test_covers('POST /error-reports', 'happy');

    $session = api_fixtures()->createUserSession();
    $response = api_client()->post('/error-reports', error_report_api_payload([
        'screenshot' => null,
        'context' => [
            'screenshot_attachment' => ['status' => 'capture_failed'],
        ],
    ]), $session['headers']);

    $response
        ->assertStatus(201)
        ->assertEnvelope()
        ->assertSuccess();

    $report = $response->data();
    expect($report['screenshot'])->toBeNull();
    expect($report['answers']['before_error'])->toBe('Opening the orders page');
    expect($report['request_error_count'])->toBe(1);
    expect($report['vue_error_count'])->toBe(1);
    expect($report['runtime_context']['screenshot_attachment'])->toMatchArray([
        'status' => 'capture_failed',
        'attached' => false,
        'mime_type' => null,
        'size_bytes' => 0,
    ]);

    error_report_api_cleanup($report);
});

it('creates error reports when an optional screenshot payload is invalid', function (): void {
    api_test_covers('POST /error-reports', 'invalid optional screenshot');

    $session = api_fixtures()->createUserSession();
    $response = api_client()->post('/error-reports', error_report_api_payload([
        'screenshot' => 'data:text/plain;base64,' . base64_encode('not an image'),
    ]), $session['headers']);

    $response
        ->assertStatus(201)
        ->assertEnvelope()
        ->assertSuccess();

    $report = $response->data();
    expect($report['screenshot'])->toBeNull();
    expect($report['runtime_context']['screenshot_attachment'])->toMatchArray([
        'status' => 'invalid',
        'attached' => false,
        'mime_type' => null,
        'size_bytes' => 0,
    ]);

    error_report_api_cleanup($report);
});
