package com.tzld.piaoquan.api;

import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.api.component.AigcApiService;
import com.tzld.piaoquan.api.component.DeepSeekApiService;
import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComRoomMapper;
import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffMapper;
import com.tzld.piaoquan.api.job.wecom.thirdpart.WeComAccountJob;
import com.tzld.piaoquan.api.job.wecom.thirdpart.WeComCreateRoomJob;
import com.tzld.piaoquan.api.job.wecom.thirdpart.WeComSendMsgJob;
import com.tzld.piaoquan.api.job.wecom.thirdpart.WeComUserDetailJob;
import com.tzld.piaoquan.api.model.bo.GoogleLLMResult;
import com.tzld.piaoquan.api.model.dto.AIResult;
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.UpdateRoomNameRequest;
import com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComRoom;
import com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaff;
import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@SpringBootTest(classes = GrowthServerApplication.class)
@Slf4j
public class WeComThirdPartTest {

    @Autowired
    WeComThirdPartyService weComThirdPartyService;
    @Autowired
    ThirdPartWeComStaffMapper staffMapper;
    @Autowired
    ThirdPartWeComRoomMapper roomMapper;
    @Autowired
    WeComSendMsgJob weComSendMsgJob;
    @Autowired
    WeComAccountJob weComAccountJob;
    @Autowired
    WeComUserDetailJob weComUserDetailJob;
    @Autowired
    WeComCreateRoomJob weComCreateRoomJob;
    @Autowired
    AigcApiService aigcApiService;
    @Autowired
    DeepSeekApiService deepSeekApiService;

    @Test
    public void checkAccountOnline() {
        weComAccountJob.checkAccountOnline("");
    }

    @Test
    public void syncUserDetail() {
        weComUserDetailJob.syncUserDetail("");
    }

    @Test
    public void syncRoomDetail() {
        weComUserDetailJob.syncRoomDetail("");
    }

    @Test
    public void autoSendAppMsg() {
        weComSendMsgJob.autoSendAppMsg("");
    }

    @Test
    public void autoOpenSendStatusJob() {
        weComSendMsgJob.autoOpenSendStatusJob("");
    }

    @Test
    public void randomRoomSendTimeJob() {
        weComSendMsgJob.randomRoomSendTimeJob("");
    }

    @Test
    public void autoCreateRoomJob() {
        weComCreateRoomJob.autoCreateRoomJob("");
    }

    @Test
    public void autoAddRoomUserJob() {
        weComCreateRoomJob.autoAddRoomUserJob("");
    }

    @Test
    public void updateRoomName() {
        ThirdPartWeComStaff staff = weComThirdPartyService.getStaffByUuid("796ab5e7ccfd81ffde0d810e5205e656");
        List<ThirdPartWeComRoom> roomList = weComThirdPartyService.getStaffRoomList(staff.getId());
        String roomNamePrefix = "票圈快看";
        int index = 1;
        for (ThirdPartWeComRoom room : roomList) {
            if (room.getName().contains(roomNamePrefix)) {
                continue;
            }
            String name = roomNamePrefix + index++;
            room.setName(name);
            roomMapper.updateByPrimaryKeySelective(room);
            UpdateRoomNameRequest request = new UpdateRoomNameRequest();
            request.setUuid(staff.getThirdUuid());
            request.setRoomid(Long.valueOf(room.getThirdRoomId()));
            request.setRoomname(name);
            weComThirdPartyService.updateRoomName(request);
        }
    }

    @Test
    public void tesGPT() {
        //String imageUrl = "http://api.e.qq.com/image/70837126_29318969717_5e4c671a4e18aaad97d940a56aeb26bf";
        //
        //// download & upload to oss
        //String fileName = String.format("supply/spider/image/%s_%d.jpg", "70837126", System.currentTimeMillis());
        //String ossImageUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, imageUrl, "image/jpeg");
        //System.out.println("ossImageUrl: " + ossImageUrl);

        String ossImageUrl = "https://rescdn.yishihui.com/supply/spider/image/70837126_1766470638729.jpg";

        // 用户问题
        String prompt = "请提取这张图片中的文字内容，仅仅提取图片中的文字，不要有其他的文字";

        // 调用API
        String result = aigcApiService.gpt("gpt-4o", prompt, null, Arrays.asList(ossImageUrl));

        System.out.println(result);
    }

    @Test
    public void tesGemini() {
        //String imageUrl = "http://api.e.qq.com/image/70837126_29318969717_5e4c671a4e18aaad97d940a56aeb26bf";
        //
        //// download & upload to oss
        //String fileName = String.format("supply/spider/image/%s_%d.jpg", "70837126", System.currentTimeMillis());
        //String ossImageUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, imageUrl, "image/jpeg");
        //System.out.println("ossImageUrl: " + ossImageUrl);

        String ossImageUrl = "https://rescdn.yishihui.com/supply/spider/image/70837126_1766470638729.jpg";

        // 用户问题
        String prompt = "识别图片里的文字，直接输出图片里的文字标题，但是不要输出图片里右下角的字，比如“点开看看”“看这里”等，仅输出标题，不要有多余的文字输出";

        // 调用API
        GoogleLLMResult result = aigcApiService.gemini("gemini-2.0-flash", prompt, new ArrayList<>(Collections.singleton(ossImageUrl)));

        System.out.println(JSONObject.toJSONString(result));
    }

    @Test
    public void testDeepSeek() {
        List<String> titles = Arrays.asList(
                "这首歌太好听了，听醉了别怪我！",
                "2025年，一首老歌送给大家，好听极了",
                "🔴致谢群主，群主辛苦了！感谢您的无私奉献！",
                "狗屁股，笑死我了!",
                "🔴菊花开了！太美了",
                "🔴这首歌，献给所有苦命人！",
                "102岁杨振宁月工资",
                "🔴少年夫妻老来伴，珍惜身边人",
                "⭕⭕⭕这才叫笑话!笑的肚子疼!哈哈哈哈!",
                "🔴人人羡慕！这才是真正的人民公社！",
                "▲80岁老大爷懂得真多，太有道理了！"
        );
        for (String title : titles) {
            String keywordPrompt =
                    "你是一位精通算法推荐逻辑的世界级短视频SEO专家。你擅长从标题中提炼出搜索量最大、用户意图最明确的“核心流量词”。\n" +
                            "\n" +
                            "# Task\n" +
                            "分析用户提供的视频标题，提炼出 2 个核心搜索关键词。\n" +
                            "\n" +
                            "# Constraints (必须严格遵守)\n" +
                            "1. **字数限制**：每个关键词严格控制在 **3个汉字以内**（包含3个字）。\n" +
                            "2. **选词逻辑**：\n" +
                            "   - 优先提取核心名词（人名/物名）或高频动词。\n" +
                            "   - 剔除虚词（如“的”、“了”、“吗”）。\n" +
                            "   - 必须与原标题强相关，能覆盖用户搜索意图。\n" +
                            "3. **输出格式**：仅输出JSON数组，**严禁**包含任何解释、Markdown标记或其他文本。\n" +
                            "\n" +
                            "# Example\n" +
                            "输入：新手如何快速学会剪映剪辑\n" +
                            "输出：[\"剪映\",\"剪辑\"]\n" +
                            "\n" +
                            "输入：宝宝感冒流鼻涕怎么办\n" +
                            "输出：[\"感冒\",\"流鼻涕\"]\n" +
                            "\n" +
                            "# Input\n" +
                            "内容是： {{text}} \n" +
                            "\n" +
                            "# Output\n" +
                            "请基于上述规则，输出最终的JSON：";
            keywordPrompt = keywordPrompt.replace("text", title);
            // 调用API
            AIResult result = deepSeekApiService.request(keywordPrompt, null, null, true);
            List<String> keywords = JSONObject.parseArray(result.getResponse().getChoices().get(0).getMessage().getContent(), String.class);

            System.out.println(String.format("title: %s, keywords: %s", title, keywords));
        }

    }

}
