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

import com.njcn.rdms.module.project.constant.ObjectActivityConstants;
import com.njcn.rdms.module.project.constant.ProjectObjectConstants;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectLifecycleActionRespVO;
import com.njcn.rdms.module.project.dal.dataobject.status.ObjectStatusModelDO;
import com.njcn.rdms.module.project.dal.dataobject.status.ObjectStatusTransitionDO;
import com.njcn.rdms.module.project.dal.mysql.status.ObjectStatusModelMapper;
import com.njcn.rdms.module.project.dal.mysql.status.ObjectStatusTransitionMapper;
import com.njcn.rdms.module.project.enums.ErrorCodeConstants;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
public class ProjectStatusViewService {

    @Resource
    private ObjectStatusModelMapper objectStatusModelMapper;
    @Resource
    private ObjectStatusTransitionMapper objectStatusTransitionMapper;

    public ProjectLifecycleView getLifecycle(String statusCode) {
        ObjectStatusModelDO statusModel = objectStatusModelMapper
                .selectByObjectTypeAndStatusCodeEnabled(ProjectObjectConstants.OBJECT_TYPE, statusCode);
        if (statusModel == null) {
            throw exception(ErrorCodeConstants.PROJECT_STATUS_MODEL_NOT_EXISTS_OR_DISABLED);
        }
        return new ProjectLifecycleView(
                statusModel.getStatusName(),
                statusModel.getTerminalFlag(),
                statusModel.getAllowEdit(),
                buildAvailableActions(statusCode)
        );
    }

    private List<ProjectLifecycleActionRespVO> buildAvailableActions(String statusCode) {
        List<ObjectStatusTransitionDO> transitions = objectStatusTransitionMapper
                .selectListByObjectTypeAndFromStatus(ProjectObjectConstants.OBJECT_TYPE, statusCode);
        if (transitions == null || transitions.isEmpty()) {
            return Collections.emptyList();
        }
        return transitions.stream()
                // auto_start 是后端内部推进动作，允许存在于流转配置中，但不应直接暴露给前端按钮。
                .filter(transition -> !Objects.equals(transition.getActionCode(), ObjectActivityConstants.PROJECT_ACTION_AUTO_START))
                .map(transition -> {
            ProjectLifecycleActionRespVO action = new ProjectLifecycleActionRespVO();
            action.setActionCode(transition.getActionCode());
            action.setActionName(transition.getActionName());
            action.setNeedReason(transition.getNeedReason());
            return action;
        }).toList();
    }

    public record ProjectLifecycleView(String statusName,
                                       Boolean terminal,
                                       Boolean allowEdit,
                                       List<ProjectLifecycleActionRespVO> availableActions) {
    }

}
