<?php

namespace onespace\tools\enum\oneaccess;

use Exception;

/**
 * The various states an Async report can be in
 * 
 * @author Gareth Palmer<gareth@one-space.co.za>
 */
enum AsyncReportStatus {
    case STATE_PENDING;
    case STATE_QUEUED;
    case STATE_PROCESSING;
    case STATE_BUILDING_ROWS;
    case STATE_BUILDING_FILES;
    case STATE_SENDING_NOTIFICATION;
    case STATE_ERROR;
    case STATE_SUCCESS;

    private function details(): array {
        return match ($this) {
            self::STATE_PENDING => [
                'id' => 0,
                'name' => 'Pending',
            ],
            self::STATE_QUEUED => [
                'id' => 1,
                'name' => 'Queued',
            ],
            self::STATE_PROCESSING => [
                'id' => 2,
                'name' => 'Processing',
            ],
            self::STATE_BUILDING_ROWS => [
                'id' => 10,
                'name' => 'Building Rows',
            ],
            self::STATE_BUILDING_FILES => [
                'id' => 11,
                'name' => 'Building Files',
            ],
            self::STATE_SENDING_NOTIFICATION => [
                'id' => 12,
                'name' => 'Sending Notification',
            ],
            self::STATE_ERROR => [
                'id' => 21,
                'name' => 'Error',
            ],
            self::STATE_SUCCESS => [
                'id' => 22,
                'name' => 'Success',
            ],
        };
    }

    public function id(): int {
        return $this->details()['id'];
    }

    public function name(): string {
        return $this->details()['name'];
    }

    public static function nameFromId(int $id): string {
        foreach (self::cases() as $case) {
            if ($case->id() === $id) {
                return $case->name();
            }
        }
        throw new Exception("Invalid Status ID");
    }
}
