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

import com.njcn.rdms.module.project.constant.SignOffConstants;
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 com.njcn.rdms.module.project.service.status.StatusActionTextResolver;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class EmployeeSignOffStatusServiceImpl implements EmployeeSignOffStatusService {

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

    @Override
    public String getInitialStatusCode() {
        ObjectStatusModelDO model = objectStatusModelMapper.selectInitialByObjectTypeEnabled(SignOffConstants.STATUS_OBJECT_TYPE);
        if (model == null || !StringUtils.hasText(model.getStatusCode())) {
            throw exception(ErrorCodeConstants.SIGN_OFF_STATUS_MODEL_NOT_EXISTS_OR_DISABLED);
        }
        return model.getStatusCode();
    }

    @Override
    public String statusName(String statusCode) {
        return statusActionTextResolver.statusName(SignOffConstants.STATUS_OBJECT_TYPE, statusCode);
    }

    @Override
    public ObjectStatusTransitionDO validateTransition(String fromStatus, String actionCode, String reason) {
        ObjectStatusTransitionDO transition = objectStatusTransitionMapper.selectByObjectTypeAndFromStatusAndAction(
                SignOffConstants.STATUS_OBJECT_TYPE, fromStatus, actionCode);
        if (transition == null) {
            throw exception(ErrorCodeConstants.SIGN_OFF_STATUS_ACTION_NOT_ALLOWED,
                    statusActionTextResolver.statusName(SignOffConstants.STATUS_OBJECT_TYPE, fromStatus),
                    statusActionTextResolver.actionName(SignOffConstants.STATUS_OBJECT_TYPE, actionCode));
        }
        if (Boolean.TRUE.equals(transition.getNeedReason()) && !StringUtils.hasText(reason)) {
            throw exception(ErrorCodeConstants.SIGN_OFF_STATUS_ACTION_REASON_REQUIRED,
                    statusActionTextResolver.actionName(SignOffConstants.STATUS_OBJECT_TYPE, actionCode));
        }
        validateStatusModel(transition.getToStatusCode());
        return transition;
    }

    @Override
    public ObjectStatusModelDO validateStatusModel(String statusCode) {
        ObjectStatusModelDO model = objectStatusModelMapper.selectByObjectTypeAndStatusCodeEnabled(
                SignOffConstants.STATUS_OBJECT_TYPE, statusCode);
        if (model == null) {
            throw exception(ErrorCodeConstants.SIGN_OFF_STATUS_MODEL_NOT_EXISTS_OR_DISABLED);
        }
        return model;
    }
}
