<?php

namespace App\Domain\Recruitment\Enums;

/**
 * Job demand lifecycle (§15). A demand only accepts candidates while Open;
 * everything before that is preparation and approval.
 */
enum JobDemandStatus: string
{
    case Draft = 'draft';
    case PendingVerification = 'pending_verification';
    case Approved = 'approved';
    case Open = 'open';
    case Paused = 'paused';
    case Filled = 'filled';
    case Closed = 'closed';
    case Cancelled = 'cancelled';

    public function label(): string
    {
        return match ($this) {
            self::Draft => 'Draft',
            self::PendingVerification => 'Pending Verification',
            self::Approved => 'Approved',
            self::Open => 'Open',
            self::Paused => 'Paused',
            self::Filled => 'Filled',
            self::Closed => 'Closed',
            self::Cancelled => 'Cancelled',
        };
    }

    /** Candidates may only be assigned to a demand that is open for intake. */
    public function acceptsCandidates(): bool
    {
        return $this === self::Open;
    }

    /** Editing terms after approval would change what candidates agreed to. */
    public function allowsTermsEditing(): bool
    {
        return in_array($this, [self::Draft, self::PendingVerification], true);
    }

    public function isFinal(): bool
    {
        return in_array($this, [self::Closed, self::Cancelled], true);
    }

    /** Approval is the point where an authorized user vouches for the demand. */
    public function requiresApproval(): bool
    {
        return in_array($this, [self::Approved, self::Open], true);
    }

    public function requiresReason(): bool
    {
        return in_array($this, [self::Paused, self::Cancelled, self::Closed], true);
    }

    /** @return array<int, self> */
    public function allowedTransitions(): array
    {
        return match ($this) {
            self::Draft => [self::PendingVerification, self::Cancelled],
            self::PendingVerification => [self::Approved, self::Draft, self::Cancelled],
            self::Approved => [self::Open, self::Draft, self::Cancelled],
            self::Open => [self::Paused, self::Filled, self::Closed, self::Cancelled],
            self::Paused => [self::Open, self::Closed, self::Cancelled],
            self::Filled => [self::Open, self::Closed],
            self::Closed, self::Cancelled => [],
        };
    }

    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');
    }
}
