<?php

namespace App\Enum\Esign;

/**
 * Derived lifecycle status of a Fintrixor document (and, aggregated, a
 * ContractRequest). rank() drives transitionStatus() so progress is monotonic.
 * Mirrors the API lifecycle: draft → sent → (opened) → viewed → signed → completed
 * (+ expired). FAILED is reserved for our own send failures.
 */
enum EsignStatus: string
{
    case DRAFT = 'draft';
    case SENT = 'sent';
    case VIEWED = 'viewed';
    case SIGNED = 'signed';
    case COMPLETED = 'completed';
    case EXPIRED = 'expired';
    case FAILED = 'failed';

    public function label(): string
    {
        return match ($this) {
            self::DRAFT => 'Draft',
            self::SENT => 'Sent',
            self::VIEWED => 'Viewed',
            self::SIGNED => 'Signed',
            self::COMPLETED => 'Completed',
            self::EXPIRED => 'Expired',
            self::FAILED => 'Failed',
        };
    }

    public function color(): string
    {
        return match ($this) {
            self::DRAFT => 'secondary',
            self::SENT => 'primary',
            self::VIEWED => 'info',
            self::SIGNED => 'warning',
            self::COMPLETED => 'success',
            self::EXPIRED, self::FAILED => 'danger',
        };
    }

    public function isTerminalSuccess(): bool
    {
        return $this === self::COMPLETED;
    }

    public function isTerminalFailure(): bool
    {
        return in_array($this, [self::EXPIRED, self::FAILED], true);
    }

    public function rank(): int
    {
        return match ($this) {
            self::DRAFT => 0,
            self::SENT => 10,
            self::VIEWED => 20,
            self::SIGNED => 30,
            self::COMPLETED => 40,
            self::EXPIRED, self::FAILED => 100,
        };
    }
}
