<?php

declare(strict_types=1);

use App\Actions\Storage\Admin\Dump\DumpContentAction;
use App\Actions\Storage\Admin\Dump\PruneDumpAction;
use App\Constants\Config\DumpConstants;
use App\Enums\Actions\ActionStatus;
use App\Models\Admin\Dump;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Storage;

uses(WithFaker::class);

test('no results', function (): void {
    $fs = Storage::fake(Config::get(DumpConstants::DISK_QUALIFIED));

    $action = new PruneDumpAction(fake()->numberBetween(2, 9));

    $pruneResults = $action->handle();

    $result = $pruneResults->toActionResult();

    $this->assertEmpty($fs->allFiles());
    $this->assertTrue($result->hasFailed());
    $this->assertDatabaseCount(Dump::class, 0);
});

test('pruned', function (): void {
    $fs = Storage::fake(Config::get(DumpConstants::DISK_QUALIFIED));

    $prunedCount = fake()->randomDigitNotNull();

    Collection::times($prunedCount, function (): void {
        Date::setTestNow(fake()->iso8601());

        $action = new DumpContentAction();

        $action->handle();
    });

    Date::setTestNow();

    $action = new PruneDumpAction(-1);

    $pruneResults = $action->handle();

    $action->then($pruneResults);

    $result = $pruneResults->toActionResult();

    $this->assertEmpty($fs->allFiles());
    $this->assertTrue($result->getStatus() === ActionStatus::PASSED);
    $this->assertEmpty(Dump::all());
});
