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 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 LanguageVerifyChain implements AbstractChianHandler<QuestionSubmitRequestDTO> {

	/**
	 * 支持的编程语言列表
	 */
	private static final List<String> SUPPORTED_LANGUAGES = Arrays.asList(
		"java",
		"cpp",
		"python",
		"go",
		"javascript",
		"c",
		"csharp",
		"rust",
		"php",
		"swift",
		"kotlin",
		"typescript",
		"ruby",
		"shell"
	);

	@Override
	public void handle(QuestionSubmitRequestDTO requestParam) {
		String language = requestParam.getLanguage();

		// 校验语言不为空
		if (StringUtils.isBlank(language)) {
			throw new ClientException("编程语言不能为空", ErrorCode.PARAMS_ERROR);
		}

		// 校验语言是否支持（不区分大小写）
		String normalizedLanguage = language.toLowerCase().trim();
		if (!SUPPORTED_LANGUAGES.contains(normalizedLanguage)) {
			throw new ClientException(
				String.format("不支持的编程语言: %s，支持的语言: %s",
					language,
					String.join(", ", SUPPORTED_LANGUAGES)),
				ErrorCode.PARAMS_ERROR
			);
		}

		log.debug("编程语言校验通过: {}", normalizedLanguage);
	}

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

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