<?php

declare(strict_types=1);

namespace App\Enums;

/**
 * Outcome of one Excel import run, tracked in `import_batches`.
 *
 * Imports are processed synchronously (there is no queue), so `Pending` and
 * `Processing` are short-lived — they exist so a batch interrupted by a PHP
 * timeout is distinguishable from one that genuinely failed.
 */
enum ImportStatus: string implements HasBadge
{
    case Pending = 'pending';
    case Processing = 'processing';
    case Completed = 'completed';
    case CompletedWithErrors = 'completed_with_errors';
    case Failed = 'failed';

    /**
     * @return array<string, string> value => label, for filter dropdowns
     */
    public static function options(): array
    {
        return array_reduce(
            self::cases(),
            static fn (array $carry, self $case): array => $carry + [$case->value => $case->label()],
            [],
        );
    }

    public function label(): string
    {
        return match ($this) {
            self::Pending => 'Pending',
            self::Processing => 'Processing',
            self::Completed => 'Completed',
            self::CompletedWithErrors => 'Completed With Errors',
            self::Failed => 'Failed',
        };
    }

    public function color(): string
    {
        return match ($this) {
            self::Pending => 'secondary',
            self::Processing => 'info',
            self::Completed => 'success',
            self::CompletedWithErrors => 'warning',
            self::Failed => 'danger',
        };
    }

    public function icon(): string
    {
        return match ($this) {
            self::Pending => 'hourglass',
            self::Processing => 'arrow-repeat',
            self::Completed => 'check-circle',
            self::CompletedWithErrors => 'exclamation-triangle',
            self::Failed => 'x-circle',
        };
    }

    /**
     * Whether the run has finished, successfully or not.
     */
    public function isFinished(): bool
    {
        return match ($this) {
            self::Completed, self::CompletedWithErrors, self::Failed => true,
            self::Pending, self::Processing => false,
        };
    }
}
