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.QuestionCreateRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 题目难度校验责任链处理器
 */
@Component
@Slf4j
public class QuestionDifficultyVerifyChain implements AbstractChianHandler<QuestionCreateRequestDTO> {

	/**
	 * 允许的难度等级
	 */
	private static final List<String> ALLOWED_DIFFICULTIES = Arrays.asList("easy", "medium", "hard");

	@Override
	public void handle(QuestionCreateRequestDTO requestParam) {
		String difficulty = requestParam.getDifficulty();

		// 校验难度不为空
		if (StringUtils.isBlank(difficulty)) {
			throw new ClientException("题目难度不能为空", ErrorCode.PARAMS_ERROR);
		}

		// 校验难度是否为允许的值（不区分大小写）
		String normalizedDifficulty = difficulty.toLowerCase().trim();
		if (!ALLOWED_DIFFICULTIES.contains(normalizedDifficulty)) {
			throw new ClientException(
				String.format("题目难度必须是以下之一: %s", String.join(", ", ALLOWED_DIFFICULTIES)),
				ErrorCode.PARAMS_ERROR
			);
		}

		log.debug("题目难度校验通过: {}", normalizedDifficulty);
	}

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

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