package com.erp.aftersale.statemachine;

import com.erp.aftersale.entity.AfterSale;
import org.springframework.stereotype.Component;
import java.util.*;

/**
 * 售后状态机 - 负责校验状态流转合法性
 *
 * 状态流转图:
 *
 *  pending -> approved  (审核通过)
 *  pending -> rejected  (审核驳回)
 *  approved -> processing (进入处理)
 *  approved -> completed (直接完成，仅退款类型)
 *  approved -> rejected  (撤销审核)
 *  processing -> received (收到退货)
 *  processing -> completed (处理完成)
 *  processing -> closed (关闭)
 *  received -> completed (完成退款)
 *  completed -> closed
 *  rejected -> pending (重新开启)
 */
@Component
public class AfterSaleStatusMachine {

    private static final Map<String, Set<String>> TRANSITIONS = new HashMap<>();

    static {
        // pending: 可流转到 approved, rejected
        TRANSITIONS.put(AfterSale.STATUS_PENDING,
                Set.of(AfterSale.STATUS_APPROVED, AfterSale.STATUS_REJECTED));

        // approved: 可流转到 processing, completed(仅退款), rejected
        TRANSITIONS.put(AfterSale.STATUS_APPROVED,
                Set.of(AfterSale.STATUS_PROCESSING, AfterSale.STATUS_COMPLETED, AfterSale.STATUS_REJECTED));

        // rejected: 可流转回 pending (重新申请)
        TRANSITIONS.put(AfterSale.STATUS_REJECTED,
                Set.of(AfterSale.STATUS_PENDING));

        // processing: 可流转到 received, completed, closed
        TRANSITIONS.put(AfterSale.STATUS_PROCESSING,
                Set.of("received", AfterSale.STATUS_COMPLETED, AfterSale.STATUS_CLOSED));

        // received: 可流转到 completed, closed
        TRANSITIONS.put("received",
                Set.of(AfterSale.STATUS_COMPLETED, AfterSale.STATUS_CLOSED));

        // completed: 可流转到 closed
        TRANSITIONS.put(AfterSale.STATUS_COMPLETED,
                Set.of(AfterSale.STATUS_CLOSED));

        // closed: 终态，不可流转
        TRANSITIONS.put(AfterSale.STATUS_CLOSED, Collections.emptySet());
    }

    /**
     * 判断从 fromStatus 到 toStatus 的流转是否合法
     */
    public boolean canTransition(String fromStatus, String toStatus) {
        if (fromStatus == null || toStatus == null) {
            return false;
        }
        Set<String> allowed = TRANSITIONS.get(fromStatus);
        if (allowed == null) {
            return false;
        }
        return allowed.contains(toStatus);
    }

    /**
     * 获取当前状态允许的所有流转目标状态
     */
    public Set<String> getAllowedTransitions(String fromStatus) {
        return TRANSITIONS.getOrDefault(fromStatus, Collections.emptySet());
    }

    /**
     * 是否是终态
     */
    public boolean isFinalStatus(String status) {
        return AfterSale.STATUS_COMPLETED.equals(status)
                || AfterSale.STATUS_CLOSED.equals(status);
    }

    /**
     * 是否允许删除
     */
    public boolean canDelete(String status) {
        return AfterSale.STATUS_PENDING.equals(status);
    }

    /**
     * 是否允许审核
     */
    public boolean canAudit(String status) {
        return AfterSale.STATUS_PENDING.equals(status);
    }

    /**
     * 是否允许执行退款
     */
    public boolean canRefund(String status) {
        return AfterSale.STATUS_APPROVED.equals(status)
                || AfterSale.STATUS_PROCESSING.equals(status);
    }
}
