<?php

declare(strict_types=1);

use App\Models\AlertDelivery;
use App\Models\AlertDestination;
use App\Models\AlertRule;
use App\Services\Alerts\AlertDestinationLastTestResolver;
use App\Support\Alerts\AlertDestinationLastTestStatusEnum;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('returns Never status when no test delivery exists', function (): void {
    [$user] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Never);
    expect($result->timestamp)->toBeNull();
    expect($result->deliveryId)->toBeNull();
});

it('returns Sent status when the latest test delivery was sent', function (): void {
    [$user] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    $delivery = AlertDelivery::factory()->test()->create([
        'workspace_id' => $workspaceId,
        'alert_destination_id' => (int) $destination->getKey(),
        'status' => AlertDelivery::STATUS_SENT,
        'sent_at' => now()->subMinutes(5),
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Sent);
    expect($result->deliveryId)->toBe((int) $delivery->getKey());
    expect($result->timestamp)->not->toBeNull();
});

it('returns Failed status when the latest test delivery failed', function (): void {
    [$user] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    $delivery = AlertDelivery::factory()->test()->create([
        'workspace_id' => $workspaceId,
        'alert_destination_id' => (int) $destination->getKey(),
        'status' => AlertDelivery::STATUS_FAILED,
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Failed);
    expect($result->deliveryId)->toBe((int) $delivery->getKey());
});

it('returns Pending status when the latest test delivery is queued', function (): void {
    [$user] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    $delivery = AlertDelivery::factory()->test()->create([
        'workspace_id' => $workspaceId,
        'alert_destination_id' => (int) $destination->getKey(),
        'status' => AlertDelivery::STATUS_QUEUED,
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Pending);
    expect($result->deliveryId)->toBe((int) $delivery->getKey());
});

it('returns Pending status when the latest test delivery is deferred', function (): void {
    [$user] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    $delivery = AlertDelivery::factory()->test()->create([
        'workspace_id' => $workspaceId,
        'alert_destination_id' => (int) $destination->getKey(),
        'status' => AlertDelivery::STATUS_DEFERRED,
        'send_after' => now()->addMinutes(5),
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Pending);
    expect($result->deliveryId)->toBe((int) $delivery->getKey());
});

it('uses the most recent test delivery when multiple exist', function (): void {
    [$user] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    AlertDelivery::factory()->test()->create([
        'workspace_id' => $workspaceId,
        'alert_destination_id' => (int) $destination->getKey(),
        'status' => AlertDelivery::STATUS_SENT,
        'sent_at' => now()->subHour(),
        'created_at' => now()->subHour(),
    ]);

    $latestDelivery = AlertDelivery::factory()->test()->create([
        'workspace_id' => $workspaceId,
        'alert_destination_id' => (int) $destination->getKey(),
        'status' => AlertDelivery::STATUS_FAILED,
        'created_at' => now()->subMinutes(5),
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Failed);
    expect($result->deliveryId)->toBe((int) $latestDelivery->getKey());
});

it('ignores non-test deliveries when resolving last test status', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $workspaceId = (int) session()->get(WorkspaceContext::SESSION_KEY);
    $destination = AlertDestination::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    $rule = AlertRule::factory()->create([
        'workspace_id' => $workspaceId,
    ]);

    AlertDelivery::factory()->create([
        'workspace_id' => $workspaceId,
        'managed_environment_id' => (int) $tenant->getKey(),
        'alert_rule_id' => (int) $rule->getKey(),
        'alert_destination_id' => (int) $destination->getKey(),
        'event_type' => 'high_drift',
        'status' => AlertDelivery::STATUS_SENT,
        'created_at' => now(),
    ]);

    $resolver = new AlertDestinationLastTestResolver;
    $result = $resolver->resolve($destination);

    expect($result->status)->toBe(AlertDestinationLastTestStatusEnum::Never);
});
