package com.njcn.rdms.module.project.constant;

import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * 对象动态常量
 *
 * 说明：
 * 1. 当前先承载产品对象首页时间线使用的 activityType / actionType 常量
 * 2. 后续项目等对象复用同类动态能力时，继续按前缀分组扩展，不单独拆分枚举
 */
public final class ObjectActivityConstants {

    private ObjectActivityConstants() {
    }

    // ========== 动态来源类型 ==========
    public static final String ACTIVITY_TYPE_STATUS = "status";
    public static final String ACTIVITY_TYPE_PRODUCT = "product";
    public static final String ACTIVITY_TYPE_MEMBER = "member";

    // ========== 审计业务类型 ==========
    public static final String PRODUCT_BIZ_TYPE = "product";
    public static final String MEMBER_BIZ_TYPE = "rdms_user_object_role";

    // ========== 产品对象动作 ==========
    public static final String PRODUCT_ACTION_CREATE = "create";
    public static final String PRODUCT_ACTION_UPDATE = "update";
    public static final String PRODUCT_ACTION_DELETE = "delete";
    public static final String PRODUCT_ACTION_CHANGE_MANAGER = "change_manager";

    // ========== 状态动作 ==========
    public static final String STATUS_ACTION_PAUSE = "pause";
    public static final String STATUS_ACTION_RESUME = "resume";
    public static final String STATUS_ACTION_ARCHIVE = "archive";
    public static final String STATUS_ACTION_ABANDON = "abandon";

    // ========== 成员动作 ==========
    public static final String MEMBER_ACTION_ADD = "add_member";
    public static final String MEMBER_ACTION_UPDATE = "update_member";
    public static final String MEMBER_ACTION_REMOVE = "remove_member";

    public static final List<String> STATUS_ACTION_TYPES = List.of(
            STATUS_ACTION_PAUSE, STATUS_ACTION_RESUME, STATUS_ACTION_ARCHIVE, STATUS_ACTION_ABANDON);

    public static final List<String> PRODUCT_TIMELINE_ACTION_TYPES = List.of(
            PRODUCT_ACTION_CREATE, PRODUCT_ACTION_CHANGE_MANAGER);

    public static final List<String> MEMBER_TIMELINE_ACTION_TYPES = List.of(
            MEMBER_ACTION_ADD, MEMBER_ACTION_UPDATE, MEMBER_ACTION_REMOVE);

    private static final Set<String> STATUS_ACTION_TYPE_SET = Set.copyOf(STATUS_ACTION_TYPES);

    public static boolean isStatusAction(String actionType) {
        return STATUS_ACTION_TYPE_SET.contains(normalize(actionType));
    }

    public static String resolveActionName(String actionType) {
        String normalizedActionType = normalize(actionType);
        if (!StringUtils.hasText(normalizedActionType)) {
            return actionType;
        }
        return switch (normalizedActionType) {
            case PRODUCT_ACTION_CREATE -> "创建";
            case PRODUCT_ACTION_UPDATE -> "更新";
            case PRODUCT_ACTION_DELETE -> "删除";
            case STATUS_ACTION_PAUSE -> "暂停";
            case STATUS_ACTION_RESUME -> "恢复";
            case STATUS_ACTION_ARCHIVE -> "归档";
            case STATUS_ACTION_ABANDON -> "废弃";
            case PRODUCT_ACTION_CHANGE_MANAGER -> "切换产品经理";
            case MEMBER_ACTION_ADD -> "新增成员";
            case MEMBER_ACTION_UPDATE -> "调整成员";
            case MEMBER_ACTION_REMOVE -> "移出成员";
            default -> normalizedActionType;
        };
    }

    private static String normalize(String value) {
        return StringUtils.hasText(value) ? value.trim() : null;
    }

}
