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

import com.njcn.rdms.module.project.constant.ObjectActivityConstants;
import com.njcn.rdms.module.project.constant.PersonalItemConstants;
import com.njcn.rdms.module.project.controller.admin.personal.vo.item.PersonalItemLifecycleActionRespVO;
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.math.BigDecimal;
import java.util.Collections;
import java.util.List;

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

@Service
public class PersonalItemStatusViewService {

    private static final String ACTION_COMPLETE = "complete";
    private static final BigDecimal COMPLETE_PROGRESS_THRESHOLD = BigDecimal.valueOf(100);

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

    public PersonalItemLifecycleView getLifecycle(String statusCode, BigDecimal progressRate) {
        ObjectStatusModelDO statusModel = objectStatusModelMapper
                .selectByObjectTypeAndStatusCodeEnabled(PersonalItemConstants.STATUS_OBJECT_TYPE, statusCode);
        if (statusModel == null) {
            throw exception(ErrorCodeConstants.PERSONAL_ITEM_STATUS_MODEL_NOT_EXISTS_OR_DISABLED);
        }
        return new PersonalItemLifecycleView(
                statusModel.getStatusName(),
                statusModel.getTerminalFlag(),
                statusModel.getAllowEdit(),
                buildAvailableActions(statusCode, progressRate)
        );
    }

    public List<PersonalItemLifecycleActionRespVO> buildAvailableActions(String statusCode, BigDecimal progressRate) {
        List<ObjectStatusTransitionDO> transitions = objectStatusTransitionMapper
                .selectListByObjectTypeAndFromStatus(PersonalItemConstants.STATUS_OBJECT_TYPE, statusCode);
        if (transitions == null || transitions.isEmpty()) {
            return Collections.emptyList();
        }
        return transitions.stream()
                .filter(transition -> !ObjectActivityConstants.TASK_ACTION_AUTO_START.equals(transition.getActionCode()))
                .filter(transition -> !ACTION_COMPLETE.equals(transition.getActionCode())
                        || isCompleteProgressSatisfied(progressRate))
                .map(transition -> {
                    PersonalItemLifecycleActionRespVO action = new PersonalItemLifecycleActionRespVO();
                    action.setActionCode(transition.getActionCode());
                    action.setActionName(transition.getActionName());
                    action.setNeedReason(transition.getNeedReason());
                    return action;
                })
                .toList();
    }

    private boolean isCompleteProgressSatisfied(BigDecimal progressRate) {
        return progressRate != null && progressRate.compareTo(COMPLETE_PROGRESS_THRESHOLD) >= 0;
    }

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