package cn.meowrain.aioj.backend.question.dto.chains;

import cn.meowrain.aioj.backend.framework.core.designpattern.chains.AbstractChianHandler;
import cn.meowrain.aioj.backend.framework.core.errorcode.ErrorCode;
import cn.meowrain.aioj.backend.framework.core.exception.ClientException;
import cn.meowrain.aioj.backend.question.common.enums.ChainMarkEnums;
import cn.meowrain.aioj.backend.question.dto.req.QuestionSubmitRequestDTO;
import cn.meowrain.aioj.backend.question.service.QuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 题目存在性校验责任链处理器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class QuestionExistVerifyChain implements AbstractChianHandler<QuestionSubmitRequestDTO> {

	private final QuestionService questionService;

	@Override
	public void handle(QuestionSubmitRequestDTO requestParam) {
		Long questionId = requestParam.getQuestionId();

		// 校验题目是否存在
		boolean exists = questionService.lambdaQuery()
			.eq(cn.meowrain.aioj.backend.question.dao.entity.Question::getId, questionId)
			.exists();

		if (!exists) {
			throw new ClientException("题目不存在，题目ID: " + questionId, ErrorCode.NOT_FOUND_ERROR);
		}

		log.debug("题目存在性校验通过，题目ID: {}", questionId);
	}

	@Override
	public String mark() {
		return ChainMarkEnums.QUESTION_SUBMIT_REQ_PARAM_VERIFY_CHAIN.getMark();
	}

	@Override
	public int getOrder() {
		return 10;
	}
}
