<?php

namespace App\Enums;

use App\Enums\Concerns\HasOptions;

/**
 * State machine for a tenant subscription.
 */
enum SubscriptionStatus: string
{
    use HasOptions;

    case Trial = 'trial';
    case PendingPayment = 'pending_payment';
    case PendingVerification = 'pending_verification';
    case Active = 'active';
    case Grace = 'grace';
    case Expired = 'expired';
    case Rejected = 'rejected';
    case Cancelled = 'cancelled';
    case Suspended = 'suspended';


    /** @return array<string,string> */
    public static function labels(): array
    {
        return [
            self::Trial->value => 'Free trial',
            self::PendingPayment->value => 'Awaiting payment',
            self::PendingVerification->value => 'Payment under review',
            self::Active->value => 'Active',
            self::Grace->value => 'Expired (grace period)',
            self::Expired->value => 'Expired',
            self::Rejected->value => 'Rejected',
            self::Cancelled->value => 'Cancelled',
            self::Suspended->value => 'Suspended',
        ];
    }

    /** @return array<string,string> */
    public static function colors(): array
    {
        return [
            self::Trial->value => 'blue',
            self::PendingPayment->value => 'amber',
            self::PendingVerification->value => 'amber',
            self::Active->value => 'green',
            self::Grace->value => 'orange',
            self::Expired->value => 'red',
            self::Rejected->value => 'red',
            self::Cancelled->value => 'gray',
            self::Suspended->value => 'red',
        ];
    }

    /** Full read + write access to the product. */
    public function grantsAccess(): bool
    {
        return $this === self::Active || $this === self::Trial;
    }

    /** Read-only access: the tenant can view + export but not create. */
    public function isReadOnly(): bool
    {
        return $this === self::Grace;
    }

    /** Nothing but the billing screens are reachable. */
    public function isLockedOut(): bool
    {
        return ! $this->grantsAccess() && ! $this->isReadOnly();
    }
}
