<?php

use App\Jobs\ReconcileAdapterRunsJob;
use App\Models\OperationRun;
use App\Models\Workspace;
use App\Services\AdapterRunReconciler;
use App\Services\OperationRunService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Queue\Middleware\WithoutOverlapping;

uses(RefreshDatabase::class);

test('reconcile adapter runs job tracks successful execution in operation runs', function (): void {
    $workspace = Workspace::factory()->create();

    $job = new class((int) $workspace->getKey(), '2026-02-15 10:00:00') extends ReconcileAdapterRunsJob
    {
        protected function reconcile(AdapterRunReconciler $reconciler): array
        {
            return [
                'candidates' => 4,
                'reconciled' => 3,
                'skipped' => 1,
            ];
        }
    };

    $job->handle(new AdapterRunReconciler, app(OperationRunService::class));

    $run = OperationRun::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->whereNull('managed_environment_id')
        ->where('type', 'ops.reconcile_adapter_runs')
        ->first();

    expect($run)->not->toBeNull();
    expect($run?->status)->toBe('completed');
    expect($run?->outcome)->toBe('succeeded');
    expect($run?->summary_counts ?? [])->toMatchArray([
        'total' => 4,
        'processed' => 4,
        'failed' => 0,
    ]);
});

test('reconcile adapter runs job tracks failure with stable code and sanitized message', function (): void {
    $workspace = Workspace::factory()->create();

    $job = new class((int) $workspace->getKey(), '2026-02-15 10:30:00') extends ReconcileAdapterRunsJob
    {
        protected function reconcile(AdapterRunReconciler $reconciler): array
        {
            throw new RuntimeException('Authorization: Bearer highly-sensitive-token-for-user@example.com');
        }
    };

    expect(fn () => $job->handle(new AdapterRunReconciler, app(OperationRunService::class)))
        ->toThrow(\RuntimeException::class);

    $run = OperationRun::query()
        ->where('workspace_id', (int) $workspace->getKey())
        ->whereNull('managed_environment_id')
        ->where('type', 'ops.reconcile_adapter_runs')
        ->first();

    expect($run)->not->toBeNull();
    expect($run?->status)->toBe('completed');
    expect($run?->outcome)->toBe('failed');

    $failure = $run?->failure_summary[0] ?? [];

    expect($failure['code'] ?? null)->toBe('ops.reconcile_adapter_runs.failed');
    expect((string) ($failure['message'] ?? ''))->not->toContain('Bearer');
    expect((string) ($failure['message'] ?? ''))->not->toContain('@example.com');
});

test('reconcile adapter runs job enforces server-side overlap middleware', function (): void {
    $job = new ReconcileAdapterRunsJob;

    $hasWithoutOverlapping = collect($job->middleware())
        ->contains(fn (mixed $middleware): bool => $middleware instanceof WithoutOverlapping);

    expect($hasWithoutOverlapping)->toBeTrue();
});
