<?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 Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('records a tenant-owned usage event with the catalog feature area', function () {
    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->for($workspace)->create();
    $user = User::factory()->create();

    $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: 42,
        metadata: [
            'checkpoint_key' => 'tenant_connected',
            'lifecycle_state' => 'completed',
        ],
    );

    expect($event->event_name)->toBe(ProductUsageEventCatalog::ONBOARDING_CHECKPOINT_COMPLETED)
        ->and($event->feature_area)->toBe('onboarding')
        ->and($event->workspace_id)->toBe((int) $workspace->getKey())
        ->and($event->managed_environment_id)->toBe((int) $tenant->getKey())
        ->and($event->user_id)->toBe((int) $user->getKey())
        ->and($event->subject_type)->toBe('managed_environment_onboarding_session')
        ->and($event->subject_id)->toBe('42')
        ->and($event->metadata)->toBe([
            'checkpoint_key' => 'tenant_connected',
            'lifecycle_state' => 'completed',
        ]);

    $this->assertDatabaseHas('product_usage_events', [
        'id' => (int) $event->getKey(),
        'workspace_id' => (int) $workspace->getKey(),
        'managed_environment_id' => (int) $tenant->getKey(),
        'user_id' => (int) $user->getKey(),
        'event_name' => ProductUsageEventCatalog::ONBOARDING_CHECKPOINT_COMPLETED,
        'feature_area' => 'onboarding',
        'subject_type' => 'managed_environment_onboarding_session',
        'subject_id' => '42',
    ]);
});

it('records a tenant-owned usage event for an archived tenant', function () {
    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->for($workspace)->archived()->create();
    $user = User::factory()->create();

    $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: 99,
        metadata: [
            'checkpoint_key' => 'verify_access',
            'lifecycle_state' => 'draft',
        ],
    );

    expect($event->workspace_id)->toBe((int) $workspace->getKey())
        ->and($event->managed_environment_id)->toBe((int) $tenant->getKey())
        ->and($event->feature_area)->toBe('onboarding');
});

it('rejects unknown event names before writing telemetry rows', function () {
    $workspace = Workspace::factory()->create();
    $tenant = ManagedEnvironment::factory()->for($workspace)->create();
    $user = User::factory()->create();

    expect(fn () => app(ProductTelemetryRecorder::class)->record(
        eventName: 'product.unknown',
        workspaceId: (int) $workspace->getKey(),
        tenantId: (int) $tenant->getKey(),
        userId: (int) $user->getKey(),
        subjectType: 'tenant',
        subjectId: 99,
    ))->toThrow(InvalidArgumentException::class, 'Unknown product telemetry event');
});