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

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

/**
 * 题目编辑时难度校验（可选字段）
 */
@Slf4j
@Component
public class QuestionEditDifficultyVerifyChain implements AbstractChianHandler<QuestionEditRequestDTO> {

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

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

		// 难度是可选的，如果为空则跳过校验
		if (StringUtils.isBlank(difficulty)) {
			return;
		}

		// 如果提供了难度，则进行校验
		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_EDIT_PARAM_VERIFY_CHAIN.getMark();
	}

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