<?php

namespace PortalPeople\virtualstock\constants;

class OrderStatus
{
    // Primary order statuses
    public const ORDER        = 'ORDER';        // New order — sent to supplier, not yet acknowledged
    public const ORDER_ACK    = 'ORDER_ACK';    // Processing — acknowledged by supplier
    public const BACKORDER    = 'BACKORDER';    // Item(s) on backorder, cannot fulfil by expected date
    public const DISPATCH     = 'DISPATCH';     // Dispatched — confirmed, picked up by carrier
    public const DELIVERY     = 'DELIVERY';     // Delivered
    public const CANCEL       = 'CANCEL';       // Cancellation requested by retailer
    public const CANCEL_ACK   = 'CANCEL_ACK';   // Cancellation acknowledged by supplier

    // Return (RMA) statuses
    public const RMA                    = 'RMA';                    // New return initiated
    public const RMA_AUTHORISED         = 'RMA_AUTHORISED';         // Return authorised by supplier
    public const RMA_COLLECTION_BOOKED  = 'RMA_COLLECTION_BOOKED';  // Collection booked
    public const RMA_REJ                = 'RMA_REJ';                // Return declined
    public const RMA_EXC                = 'RMA_EXC';                // Return rejected — received but not refundable
    public const RMA_REC                = 'RMA_REC';                // Return approved — received and refundable

    /** Human-readable labels keyed by status constant */
    public const LABELS = [
        self::ORDER                   => 'New Order',
        self::ORDER_ACK               => 'Processing',
        self::BACKORDER               => 'Back Order',
        self::DISPATCH                => 'Dispatched',
        self::DELIVERY                => 'Delivered',
        self::CANCEL                  => 'Cancellation Requested',
        self::CANCEL_ACK              => 'Cancellation Acknowledged',
        self::RMA                     => 'Return Requested',
        self::RMA_AUTHORISED          => 'Return Authorised',
        self::RMA_COLLECTION_BOOKED   => 'Return Collection Booked',
        self::RMA_REJ                 => 'Return Declined',
        self::RMA_EXC                 => 'Return Rejected',
        self::RMA_REC                 => 'Return Approved',
    ];

    /**
     * Valid next statuses for each status (supplier-side transitions only).
     *
     * @var array<string, string[]>
     */
    public const TRANSITIONS = [
        self::ORDER      => [self::ORDER_ACK, self::CANCEL_ACK],
        self::ORDER_ACK  => [self::BACKORDER, self::DISPATCH, self::CANCEL, self::CANCEL_ACK],
        self::BACKORDER  => [self::DISPATCH, self::CANCEL, self::CANCEL_ACK],
        self::DISPATCH   => [self::DELIVERY, self::RMA],
        self::DELIVERY   => [self::RMA],
        self::CANCEL     => [self::CANCEL_ACK],
        self::CANCEL_ACK => [],
        self::RMA        => [self::RMA_AUTHORISED, self::RMA_REJ],
        self::RMA_AUTHORISED         => [self::RMA_COLLECTION_BOOKED],
        self::RMA_COLLECTION_BOOKED  => [self::RMA_REC, self::RMA_EXC],
        self::RMA_REJ    => [],
        self::RMA_EXC    => [],
        self::RMA_REC    => [],
    ];

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

    /** @return string[] */
    public static function nextStatuses(string $status): array
    {
        return self::TRANSITIONS[$status] ?? [];
    }

    public static function canTransitionTo(string $from, string $to): bool
    {
        return in_array($to, self::TRANSITIONS[$from] ?? [], true);
    }
}
