<?php

namespace PortalPeople\virtualstock\constants;

class ShipmentStatus
{
    public const PENDING           = 'PENDING';           // New shipment — pending tracking
    public const INFORECEIVED      = 'INFORECEIVED';      // Carrier received tracking number
    public const INTRANSIT         = 'INTRANSIT';         // Shipment on the way
    public const OUTFORDELIVERY    = 'OUTFORDELIVERY';    // Ready for delivery/pickup
    public const FAILEDATTEMPT     = 'FAILEDATTEMPT';     // Delivery failed, carrier will retry
    public const EXCEPTION         = 'EXCEPTION';         // Delivery exception or system failure
    public const TRACKINGEXCEPTION = 'TRACKINGEXCEPTION'; // No update from carrier in 48 hours
    public const DELIVERED         = 'DELIVERED';         // Successfully delivered
    public const UNTRACKED         = 'UNTRACKED';         // No further tracking updates

    public const LABELS = [
        self::PENDING           => 'Pending',
        self::INFORECEIVED      => 'Info Received',
        self::INTRANSIT         => 'In Transit',
        self::OUTFORDELIVERY    => 'Out for Delivery',
        self::FAILEDATTEMPT     => 'Failed Attempt',
        self::EXCEPTION         => 'Exception',
        self::TRACKINGEXCEPTION => 'Tracking Exception',
        self::DELIVERED         => 'Delivered',
        self::UNTRACKED         => 'Untracked',
    ];

    /** Statuses that indicate the shipment is still moving */
    public const ACTIVE_STATUSES = [
        self::PENDING,
        self::INFORECEIVED,
        self::INTRANSIT,
        self::OUTFORDELIVERY,
        self::FAILEDATTEMPT,
    ];

    /** Statuses that indicate the shipment has reached a terminal state */
    public const TERMINAL_STATUSES = [
        self::DELIVERED,
        self::UNTRACKED,
        self::EXCEPTION,
        self::TRACKINGEXCEPTION,
    ];

    public static function label(string $status): string
    {
        return self::LABELS[$status] ?? $status;
    }

    public static function isValid(string $status): bool
    {
        return array_key_exists($status, self::LABELS);
    }

    public static function isTerminal(string $status): bool
    {
        return in_array($status, self::TERMINAL_STATUSES, true);
    }
}
