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 PROJECT_BIZ_TYPE = "project";
    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 PROJECT_ACTION_CREATE = "create";
    public static final String PROJECT_ACTION_UPDATE = "update";
    public static final String PROJECT_ACTION_DELETE = "delete";
    public static final String PROJECT_ACTION_CHANGE_MANAGER = "change_manager";
    public static final String PROJECT_ACTION_AUTO_START = "auto_start";
    public static final String PROJECT_ACTION_COMPLETE = "complete";

    // ========== 项目自动推进触发动作 ==========
    public static final String PROJECT_TRIGGER_CREATE_EXECUTION = "create_execution";
    public static final String PROJECT_TRIGGER_CREATE_TASK = "create_task";
    public static final String PROJECT_TRIGGER_SCHEDULE_REQUIREMENT = "schedule_requirement";
    /**
     * 项目自动开始触发动作 —— 由执行 auto_start 反向触发。
     * 与 {@link #EXECUTION_TRIGGER_AUTO_START} 同语义；项目侧用此名走 AUTO_START_TRIGGERS 白名单。
     */
    public static final String PROJECT_TRIGGER_EXECUTION_AUTO_START = "execution_auto_start";

    // ========== 任务自动推进触发动作 ==========
    /**
     * 任务自动开始动作编码 —— owner 或协办人填报工时时由后端自动触发的状态流转动作。
     * 对应 rdms_object_status_transition (object_type=task, action_code=auto_start,
     * from=pending → to=active)。
     * 修改 DB 该条记录的 action_code 时必须同步修改本常量；本常量被
     * ProjectTaskServiceImpl#internalAutoStartByWorklog 引用。
     * 后续若再增加"任务自动完成 / 执行自动开始"等同类语义，按 *_ACTION_AUTO_* 命名加在本分组。
     */
    public static final String TASK_ACTION_AUTO_START = "auto_start";

    // ========== 状态动作 ==========
    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";
    /**
     * 复活动作：原 INACTIVE 成员行被重新激活（status: 1 → 0），用于把"再次新增 / update 改 role 命中老 INACTIVE 行"路径
     * 跟物理"新增 / 更新"的 audit 语义区分开。createXxxMember 命中 INACTIVE 三元组复活老行时使用本动作，避免 ADD 语义误用。
     */
    public static final String MEMBER_ACTION_REACTIVATE = "reactivate_member";
    public static final String EXECUTION_ACTION_CREATE = "create_execution_entity";
    public static final String EXECUTION_ACTION_UPDATE = "update_execution_entity";
    public static final String EXECUTION_ACTION_DELETE = "delete_execution_entity";
    public static final String EXECUTION_ACTION_CHANGE_OWNER = "change_execution_owner";
    public static final String EXECUTION_ASSIGNEE_ACTION_ADD = "add_execution_assignee";
    public static final String EXECUTION_ASSIGNEE_ACTION_REMOVE = "remove_execution_assignee";
    public static final String TASK_ACTION_CREATE = "create_task_entity";
    public static final String TASK_ACTION_UPDATE = "update_task_entity";
    public static final String TASK_ACTION_DELETE = "delete_task_entity";
    public static final String PERSONAL_ITEM_ACTION_CREATE = "create_personal_item";
    public static final String PERSONAL_ITEM_ACTION_UPDATE = "update_personal_item";
    public static final String PERSONAL_ITEM_ACTION_DELETE = "delete_personal_item";

    // ========== 任务协办人事件类型（B 模型 - 多行周期记录） ==========
    public static final String TASK_ASSIGNEE_ACTION_JOIN = "join";
    public static final String TASK_ASSIGNEE_ACTION_INACTIVE = "inactive";

    // ========== 执行协办人事件类型（B 模型 - 多行周期记录） ==========
    public static final String EXECUTION_ASSIGNEE_LOG_ACTION_JOIN = "join";
    public static final String EXECUTION_ASSIGNEE_LOG_ACTION_INACTIVE = "inactive";
    public static final String EXECUTION_ASSIGNEE_LOG_ACTION_OWNER_TRANSFER_IN = "owner_transfer_in";
    public static final String EXECUTION_ASSIGNEE_LOG_ACTION_OWNER_TRANSFER_OUT = "owner_transfer_out";

    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, MEMBER_ACTION_REACTIVATE);

    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 PROJECT_ACTION_AUTO_START -> "开始推进";
            case PROJECT_TRIGGER_CREATE_EXECUTION -> "创建执行";
            case PROJECT_TRIGGER_CREATE_TASK -> "创建任务";
            case PROJECT_TRIGGER_SCHEDULE_REQUIREMENT -> "项目需求排期";
            case PROJECT_TRIGGER_EXECUTION_AUTO_START -> "执行自动开始";
            case EXECUTION_ACTION_CREATE -> "创建执行";
            case EXECUTION_ACTION_UPDATE -> "更新执行";
            case EXECUTION_ACTION_DELETE -> "删除执行";
            case EXECUTION_ACTION_CHANGE_OWNER -> "变更执行负责人";
            case EXECUTION_ASSIGNEE_ACTION_ADD -> "新增执行协办人";
            case EXECUTION_ASSIGNEE_ACTION_REMOVE -> "移出执行协办人";
            case TASK_ACTION_CREATE -> "创建任务";
            case TASK_ACTION_UPDATE -> "更新任务";
            case TASK_ACTION_DELETE -> "删除任务";
            case PERSONAL_ITEM_ACTION_CREATE -> "创建个人事项";
            case PERSONAL_ITEM_ACTION_UPDATE -> "更新个人事项";
            case PERSONAL_ITEM_ACTION_DELETE -> "删除个人事项";
            case TASK_ASSIGNEE_ACTION_JOIN -> "加入";
            case TASK_ASSIGNEE_ACTION_INACTIVE -> "退出";
            case EXECUTION_ASSIGNEE_LOG_ACTION_OWNER_TRANSFER_IN -> "转入负责人";
            case EXECUTION_ASSIGNEE_LOG_ACTION_OWNER_TRANSFER_OUT -> "转出负责人";
            case "start" -> "开始";
            case "block" -> "阻塞";
            case "complete" -> "完成";
            case "cancel" -> "取消";
            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 -> "移出成员";
            case MEMBER_ACTION_REACTIVATE -> "重新激活成员";
            default -> normalizedActionType;
        };
    }

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

}
