<?php

declare(strict_types=1);

use App\Filament\Resources\AlertRuleResource;
use App\Models\AlertRule;
use App\Models\ReviewPack;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    Storage::fake('exports');
});

it('expires ready packs past retention and deletes their files', function (): void {
    [$user, $tenant] = createUserWithTenant();

    $filePath = 'review-packs/test-expired.zip';
    Storage::disk('exports')->put($filePath, 'fake content');

    $pack = ReviewPack::factory()->ready()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'file_path' => $filePath,
        'expires_at' => now()->subDay(),
    ]);

    $this->artisan('tenantpilot:review-pack:prune')
        ->assertSuccessful();

    $pack->refresh();
    expect($pack->status)->toBe(ReviewPack::STATUS_EXPIRED)
        ->and(Storage::disk('exports')->exists($filePath))->toBeFalse();
});

it('does not expire ready packs with future retention', function (): void {
    [$user, $tenant] = createUserWithTenant();

    $filePath = 'review-packs/test-future.zip';
    Storage::disk('exports')->put($filePath, 'fake content');

    $pack = ReviewPack::factory()->ready()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'file_path' => $filePath,
        'expires_at' => now()->addDays(30),
    ]);

    $this->artisan('tenantpilot:review-pack:prune')
        ->assertSuccessful();

    $pack->refresh();
    expect($pack->status)->toBe(ReviewPack::STATUS_READY)
        ->and(Storage::disk('exports')->exists($filePath))->toBeTrue();
});

it('hard-deletes expired packs past grace period', function (): void {
    [$user, $tenant] = createUserWithTenant();

    $graceDays = config('tenantpilot.review_pack.hard_delete_grace_days', 30);

    $pack = ReviewPack::factory()->expired()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'updated_at' => now()->subDays($graceDays + 5),
    ]);

    $this->artisan('tenantpilot:review-pack:prune --hard-delete')
        ->assertSuccessful();

    expect(ReviewPack::query()->whereKey($pack->getKey())->exists())->toBeFalse();
});

it('keeps expired packs within grace period when hard-deleting', function (): void {
    [$user, $tenant] = createUserWithTenant();

    $pack = ReviewPack::factory()->expired()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'updated_at' => now()->subDays(5),
    ]);

    $this->artisan('tenantpilot:review-pack:prune --hard-delete')
        ->assertSuccessful();

    expect(ReviewPack::query()->whereKey($pack->getKey())->exists())->toBeTrue();
});

it('outputs correct counts', function (): void {
    [$user, $tenant] = createUserWithTenant();

    // 2 packs past retention → expired
    ReviewPack::factory()->count(2)->ready()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'expires_at' => now()->subDays(3),
    ]);

    $this->artisan('tenantpilot:review-pack:prune')
        ->expectsOutputToContain('2 pack(s) expired, 0 pack(s) hard-deleted')
        ->assertSuccessful();
});

it('outputs hard-delete counts when option is passed', function (): void {
    [$user, $tenant] = createUserWithTenant();

    $graceDays = config('tenantpilot.review_pack.hard_delete_grace_days', 30);

    // 1 pack past retention → expired
    ReviewPack::factory()->ready()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'expires_at' => now()->subDay(),
    ]);

    // 1 expired pack past grace → hard-deleted
    ReviewPack::factory()->expired()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'updated_at' => now()->subDays($graceDays + 10),
    ]);

    $this->artisan('tenantpilot:review-pack:prune --hard-delete')
        ->expectsOutputToContain('1 pack(s) expired, 1 pack(s) hard-deleted')
        ->assertSuccessful();
});

it('does not hard-delete without the flag', function (): void {
    [$user, $tenant] = createUserWithTenant();

    $graceDays = config('tenantpilot.review_pack.hard_delete_grace_days', 30);

    $pack = ReviewPack::factory()->expired()->create([
        'managed_environment_id' => $tenant->id,
        'workspace_id' => $tenant->workspace_id,
        'initiated_by_user_id' => $user->id,
        'updated_at' => now()->subDays($graceDays + 10),
    ]);

    $this->artisan('tenantpilot:review-pack:prune')
        ->assertSuccessful();

    expect(ReviewPack::query()->whereKey($pack->getKey())->exists())->toBeTrue();
});

it('AlertRule form shows sla_due option', function (): void {
    $options = AlertRuleResource::eventTypeOptions();

    expect($options)->toHaveKey(AlertRule::EVENT_SLA_DUE)
        ->and($options[AlertRule::EVENT_SLA_DUE])->toBe('SLA due')
        ->and($options)->toHaveKey(AlertRule::EVENT_HIGH_DRIFT)
        ->and($options)->toHaveKey(AlertRule::EVENT_COMPARE_FAILED)
        ->and($options)->toHaveKey(AlertRule::EVENT_PERMISSION_MISSING)
        ->and($options)->toHaveKey(AlertRule::EVENT_ENTRA_ADMIN_ROLES_HIGH);
});
