package top.tqx.demo_1.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Data
@Schema(description = "题目生成请求")
public class ExamGenerateRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("request_id")
    @Schema(description = "请求ID，用于幂等性")
    private String requestId;

    @JsonProperty("question_count")
    @Schema(description = "题目数量")
    private Integer questionCount;

    @JsonProperty("question_types")
    @Schema(description = "题型及数量配置（不提供则使用智能生成模式，AI自动判断题型和数量），支持 List<String> 或 Map<String, Integer>")
    private Object questionTypes; // 支持 List<String> 或 Map<String, Integer>

    @JsonProperty("difficulty")
    @Schema(description = "难度等级（1-5），默认3")
    private Integer difficulty;

    @JsonProperty("collection_name")
    @Schema(description = "向量库名称")
    private String collectionName;

    @JsonProperty("collection")
    @Schema(description = "向量库，支持单个字符串或字符串数组")
    private Object collection; // 支持单个字符串或字符串数组

    @JsonProperty("file_path")
    @Schema(description = "文件路径（file_id 和 file_path 二选一）")
    private String filePath;

    @JsonProperty("file_id")
    @Schema(description = "文件ID（file_id 和 file_path 二选一，推荐使用）")
    private Long fileId; // 通过文件ID指定文件

    @JsonProperty("tags")
    @Schema(description = "标签列表")
    private List<String> tags;

    @JsonProperty("question_scores")
    @Schema(description = "自定义题型分数配置，仅在指定题型模式下有效")
    private Map<String, BigDecimal> questionScores; // 各题型分数配置

    @Data
    public static class QuestionContent implements Serializable {
        private static final long serialVersionUID = 1L;

        @JsonProperty("stem")
        private String stem;

        @JsonProperty("data")
        private Map<String, Object> data;

        @JsonProperty("answer")
        private Object answer;

        @JsonProperty("explanation")
        private String explanation;
    }

    @Data
    public static class SourceInfo implements Serializable {
        private static final long serialVersionUID = 1L;

        @JsonProperty("chunk_id")
        private String chunkId;

        @JsonProperty("page")
        private Integer page;

        @JsonProperty("section")
        private String section;

        @JsonProperty("snippet")
        private String snippet;
    }

    @Data
    public static class SourceTrace implements Serializable {
        private static final long serialVersionUID = 1L;

        @JsonProperty("document_name")
        private String documentName;

        @JsonProperty("chunk_ids")
        private List<String> chunkIds;

        @JsonProperty("chunks_count")
        private Integer chunksCount;

        @JsonProperty("page_numbers")
        private List<Integer> pageNumbers;

        @JsonProperty("sources")
        private List<SourceInfo> sources;
    }

    @Data
    public static class Question implements Serializable {
        private static final long serialVersionUID = 1L;

        @JsonProperty("question_type")
        private String questionType;

        @JsonProperty("type")
        private String type;

        @JsonProperty("difficulty")
        private Integer difficulty;

        @JsonProperty("content")
        private QuestionContent content;

        @JsonProperty("source_trace")
        private SourceTrace sourceTrace;

        @JsonProperty("stem")
        private String stem;

        @JsonProperty("answer")
        private Object answer;

        @JsonProperty("explanation")
        private String explanation;

        @JsonProperty("options")
        private Object options;

        @JsonProperty("score")
        private BigDecimal score; // 单个题目分数

        public String getQuestionType() {
            if (questionType != null && !questionType.isEmpty()) {
                return questionType;
            }
            return type;
        }

        public QuestionContent getContent() {
            if (content != null && content.getStem() != null && !content.getStem().isEmpty()) {
                return content;
            }
            QuestionContent newContent = new QuestionContent();
            newContent.setStem(this.stem);
            newContent.setAnswer(this.answer);
            newContent.setExplanation(this.explanation);
            if (this.options != null) {
                newContent.setData(java.util.Collections.singletonMap("options", this.options));
            }
            return newContent;
        }
    }
}