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.dao.entity.Question;
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 QuestionStatusVerifyChain implements AbstractChianHandler<QuestionSubmitRequestDTO> {

	private final QuestionService questionService;

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

		// 查询题目详情
		Question question = questionService.getById(questionId);

		if (question == null) {
			throw new ClientException("题目不存在", ErrorCode.NOT_FOUND_ERROR);
		}

		// 校验题目是否可用（未删除、状态正常）
		if (question.getIsDelete() != null && question.getIsDelete() == 1) {
			throw new ClientException("题目已被删除，无法提交", ErrorCode.FORBIDDEN_ERROR);
		}

		// 可以添加更多状态校验，比如题目是否草稿状态、是否暂停提交等
		// if (question.getStatus() != null && question.getStatus() != 1) {
		//     throw new ClientException("题目当前不可用", ErrorCode.FORBIDDEN_ERROR);
		// }

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

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

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