package org.qinan.safetyeval.app.support;

import org.qinan.safetyeval.domain.constant.QualFilingStatusEnum;
import org.qinan.safetyeval.domain.entity.QualFilingEntity;
import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.springframework.stereotype.Component;

/**
 * 备案状态操作校验
 */
@Component
public class QualFilingStatusGuard {

    public void ensureEditable(QualFilingEntity filing) {
        if (filing == null) {
            throw new BizException(ErrorCode.QUAL_FILING_NOT_FOUND);
        }
        if (!QualFilingStatusEnum.isEditable(filing.getFilingStatusCode())) {
            throw new BizException(ErrorCode.QUAL_FILING_STATUS_INVALID);
        }
    }

    public void ensureSubmittable(QualFilingEntity filing) {
        if (filing == null) {
            throw new BizException(ErrorCode.QUAL_FILING_NOT_FOUND);
        }
        Integer code = filing.getFilingStatusCode();
        if (code == null
                || (code != QualFilingStatusEnum.DRAFT.getCode()
                && code != QualFilingStatusEnum.REJECTED.getCode())) {
            throw new BizException(ErrorCode.QUAL_FILING_STATUS_INVALID);
        }
    }

    public void ensureFiled(QualFilingEntity filing) {
        if (filing == null) {
            throw new BizException(ErrorCode.QUAL_FILING_NOT_FOUND);
        }
        if (!QualFilingStatusEnum.isFiled(filing.getFilingStatusCode())) {
            throw new BizException(ErrorCode.QUAL_FILING_STATUS_INVALID);
        }
    }

    public void ensureNotChangeReviewing(QualFilingEntity filing) {
        if (filing != null
                && filing.getFilingStatusCode() != null
                && filing.getFilingStatusCode() == QualFilingStatusEnum.CHANGE_REVIEWING.getCode()) {
            throw new BizException(ErrorCode.QUAL_FILING_CHANGE_IN_PROGRESS);
        }
    }
}
