<?php

namespace App\Domain\Agents\Enums;

/** §12 agent statuses and the transitions allowed between them. */
enum AgentStatus: string
{
    case PendingApproval = 'pending_approval';
    case Active = 'active';
    case Suspended = 'suspended';
    case Inactive = 'inactive';
    case Terminated = 'terminated';
    case Blacklisted = 'blacklisted';

    public function label(): string
    {
        return match ($this) {
            self::PendingApproval => 'Pending Approval',
            self::Active => 'Active',
            self::Suspended => 'Suspended',
            self::Inactive => 'Inactive',
            self::Terminated => 'Terminated',
            self::Blacklisted => 'Blacklisted',
        };
    }

    /** Only an active agent may register applicants or earn new commission. */
    public function canOperate(): bool
    {
        return $this === self::Active;
    }

    /** Terminal states are only left by an explicit reinstatement. */
    public function isTerminal(): bool
    {
        return in_array($this, [self::Terminated, self::Blacklisted], true);
    }

    /** @return array<int, self> */
    public function allowedTransitions(): array
    {
        return match ($this) {
            self::PendingApproval => [self::Active, self::Inactive, self::Terminated, self::Blacklisted],
            self::Active => [self::Suspended, self::Inactive, self::Terminated, self::Blacklisted],
            self::Suspended => [self::Active, self::Inactive, self::Terminated, self::Blacklisted],
            self::Inactive => [self::Active, self::Terminated, self::Blacklisted],
            self::Terminated, self::Blacklisted => [self::Active],
        };
    }

    public function canTransitionTo(self $target): bool
    {
        return in_array($target, $this->allowedTransitions(), true);
    }

    /** @return array<int, string> */
    public static function values(): array
    {
        return array_column(self::cases(), 'value');
    }
}
