<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Models\Workspace;
use App\Support\ProductTelemetry\ProductTelemetryRecorder;
use App\Support\ProductTelemetry\ProductUsageEventCatalog;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

dataset('unsafeProductTelemetryMetadata', [
    'email address' => ['checkpoint_key', 'operator@example.com'],
    'free text' => ['checkpoint_key', 'completed checkpoint'],
    'nested payload' => ['checkpoint_key', ['raw' => 'payload']],
]);

it('normalizes timestamp metadata into ISO strings', function () {
    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->for($workspace)->create();
    $user = User::factory()->create();
    $completedAt = CarbonImmutable::parse('2026-04-26T19:40:38+00:00');

    $event = app(ProductTelemetryRecorder::class)->record(
        eventName: ProductUsageEventCatalog::ONBOARDING_CHECKPOINT_COMPLETED,
        workspaceId: (int) $workspace->getKey(),
        tenantId: (int) $tenant->getKey(),
        userId: (int) $user->getKey(),
        subjectType: 'managed_environment_onboarding_session',
        subjectId: 7,
        metadata: [
            'checkpoint_key' => 'tenant_connected',
            'lifecycle_state' => 'completed',
            'completed_at' => $completedAt,
        ],
    );

    expect($event->metadata)->toBe([
        'checkpoint_key' => 'tenant_connected',
        'lifecycle_state' => 'completed',
        'completed_at' => $completedAt->toIso8601String(),
    ]);
});

it('rejects metadata keys that are not declared for the event', function () {
    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->for($workspace)->create();
    $user = User::factory()->create();

    expect(fn () => app(ProductTelemetryRecorder::class)->record(
        eventName: ProductUsageEventCatalog::OPERATIONS_STARTED,
        workspaceId: (int) $workspace->getKey(),
        tenantId: (int) $tenant->getKey(),
        userId: (int) $user->getKey(),
        subjectType: 'operation_run',
        subjectId: 9,
        metadata: [
            'unknown_key' => 'environment.review_pack.generate',
        ],
    ))->toThrow(InvalidArgumentException::class, 'Unsupported telemetry metadata keys');
});

it('rejects unsafe metadata values', function (string $key, mixed $value) {
    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->for($workspace)->create();
    $user = User::factory()->create();

    expect(fn () => app(ProductTelemetryRecorder::class)->record(
        eventName: ProductUsageEventCatalog::ONBOARDING_CHECKPOINT_COMPLETED,
        workspaceId: (int) $workspace->getKey(),
        tenantId: (int) $tenant->getKey(),
        userId: (int) $user->getKey(),
        subjectType: 'managed_environment_onboarding_session',
        subjectId: 77,
        metadata: [
            $key => $value,
            'lifecycle_state' => 'completed',
        ],
    ))->toThrow(InvalidArgumentException::class);
})->with('unsafeProductTelemetryMetadata');