package com.kexue.skills.entity.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SkillRequest implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String model;
    private List<Message> messages;
    private double temperature;
    private int max_tokens;
    private ResponseFormat response_format;

    public SkillRequest(boolean useDefaultSettings) {
        if (useDefaultSettings) {
            this.model = "deepseek-chat";
            this.messages = new ArrayList<>();
            
            Message systemMessage = new Message();
            systemMessage.setRole("system");
            systemMessage.setContent("你是一个专业的AI技能设计助手。请严格按照指定的JSON格式输出，仅包含要求的字段，以中文形式返回。");
            this.messages.add(systemMessage);
            
            Message userMessage = new Message();
            userMessage.setRole("user");
            userMessage.setContent("主题：我想要做一个将数据库设计表转换成sql schema语言并迁移到数据库服务器中的skill。请根据agent skills撰写规范帮我生成这个skill的名称、描述，并从数据库中选择一个或者多个标签。输出json格式，仅输出以上所提到的名称、描述、标签，节点名称分别为name、description、tags，节点内容以中文形式返回。");
            this.messages.add(userMessage);
            
            this.temperature = 0.3;
            this.max_tokens = 500;
            
            this.response_format = new ResponseFormat();
            this.response_format.setType("json_object");
        }
    }

    public SkillRequest(boolean useDefaultSettings, String model, Double temperature, Integer maxTokens, String prompt, String tagsList) {
        if (useDefaultSettings) {
            this.model = model;
            this.messages = new ArrayList<>();
            
            Message systemMessage = new Message();
            systemMessage.setRole("system");
            systemMessage.setContent("你是一个专业的AI技能设计助手。请严格按照指定的JSON格式输出，仅包含要求的字段，以中文形式返回。");
            this.messages.add(systemMessage);
            
            Message userMessage = new Message();
            userMessage.setRole("user");
            userMessage.setContent("主题："+ prompt +"。请根据agent skills撰写规范帮我生成这个skill的名称、描述，并从以下标签列表中选择一个或者多个标签：\"" + tagsList + "\"。输出json格式，仅输出以上所提到的名称、描述、标签，节点名称分别为name、description、tags，节点内容以中文形式返回，tags只需要返回序号数组");
            this.messages.add(userMessage);
            
            this.temperature = temperature;
            this.max_tokens = maxTokens;
            
            this.response_format = new ResponseFormat();
            this.response_format.setType("json_object");
        }
    }

    public SkillRequest(boolean useDefaultSettings, String model, String systemContent,String userContent,Double temperature, Integer maxTokens,String type) {
        if (useDefaultSettings) {
            this.model = model;
            this.messages = new ArrayList<>();

            Message systemMessage = new Message();
            systemMessage.setRole("system");
            systemMessage.setContent(systemContent);
            this.messages.add(systemMessage);

            Message userMessage = new Message();
            userMessage.setRole("user");
            userMessage.setContent(userContent);
            this.messages.add(userMessage);

            this.temperature = temperature;
            this.max_tokens = maxTokens;

            this.response_format = new ResponseFormat();
            this.response_format.setType(type);
        }
    }

    // 新的构造方法，支持文件URL列表
    public SkillRequest(String model, String systemContent, String prompt, List<String> fileUrls, double temperature, int maxTokens) {
        this.model = model;
        this.messages = new ArrayList<>();
        
        Message systemMessage = new Message();
        systemMessage.setRole("system");
        systemMessage.setContent(systemContent);
        this.messages.add(systemMessage);
        
        // 构建包含文件URL的用户消息
        List<MessageContent> messageContents = new ArrayList<>();
        
        // 添加文件URL
        if (fileUrls != null && !fileUrls.isEmpty()) {
            for (String fileUrl : fileUrls) {
                MessageContent fileContent = new MessageContent();
                fileContent.setType("file_url");
                FileUrl fileUrlObj = new FileUrl();
                fileUrlObj.setUrl(fileUrl);
                fileContent.setFile_url(fileUrlObj);
                messageContents.add(fileContent);
            }
        }
        
        // 添加文本内容
        MessageContent textContent = new MessageContent();
        textContent.setType("text");
        textContent.setText(prompt);
        messageContents.add(textContent);
        
        Message userMessage = new Message();
        userMessage.setRole("user");
        userMessage.setContent(messageContents);
        this.messages.add(userMessage);
        
        this.temperature = temperature;
        this.max_tokens = maxTokens;
        
        this.response_format = new ResponseFormat();
        this.response_format.setType("json_object");
    }

    public static SkillRequest createDefault() {
        return new SkillRequest(true);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Message implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private String role;
        private Object content; // 可以是String或List<MessageContent>
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MessageContent implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private String type;
        private String text;
        private FileUrl file_url;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class FileUrl implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private String url;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ResponseFormat implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private String type;
    }
}