package com.snack.server.module.order.enums;

import lombok.Getter;

import java.util.Set;

/**
 * 订单状态枚举（订单状态机）
 *
 * 状态流转：
 *   0 待付款 ─┬─► 1 待发货 ──► 2 待收货 ──► 3 已完成
 *            │                  │
 *            └─► 4 已取消       ├─► 3 已完成
 *            │                  └─► 5 已退款
 *            └─► 5 已退款（可选流程）
 */
@Getter
public enum OrderStatusEnum {

    PENDING_PAY(0, "待付款"),
    PENDING_SHIP(1, "待发货"),
    PENDING_RECEIVE(2, "待收货"),
    COMPLETED(3, "已完成"),
    CANCELLED(4, "已取消"),
    REFUNDED(5, "已退款");

    private final Integer code;
    private final String desc;

    OrderStatusEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    /**
     * 判断是否可以从 fromStatus 流转到 toStatus
     */
    public static boolean canTransition(Integer from, Integer to) {
        if (from == null || to == null) return false;
        // 已完成 / 已取消 / 已退款 是终态
        Set<Integer> terminal = Set.of(COMPLETED.code, CANCELLED.code, REFUNDED.code);
        if (terminal.contains(from)) return false;
        switch (from) {
            case 0: // 待付款
                return to == 1 || to == 4 || to == 5;
            case 1: // 待发货
                return to == 2 || to == 5; // 商家可主动取消（变已退款）
            case 2: // 待收货
                return to == 3 || to == 5;
            default:
                return false;
        }
    }

    /**
     * 状态标签颜色（Element Plus 风格）
     */
    public String tagType() {
        return switch (this) {
            case PENDING_PAY     -> "warning";
            case PENDING_SHIP    -> "primary";
            case PENDING_RECEIVE -> "success";
            case COMPLETED       -> "success";
            case CANCELLED       -> "info";
            case REFUNDED        -> "danger";
        };
    }
}
