package com.slodon.b2b2c.investment.controller;

import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;

import com.slodon.b2b2c.investment.bean.dto.RivalDTO;
import com.slodon.b2b2c.investment.bean.dto.SearchVolumeDTO;
import com.slodon.b2b2c.investment.bean.vo.KeyWordPartVO;
import com.slodon.b2b2c.investment.bean.vo.RivalPartVO;
import com.slodon.b2b2c.investment.bean.vo.SuggestionVO;
import com.slodon.b2b2c.investment.model.AnalysisModel;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


/**
 * @author sunshihao
 * @version 1.0
 * @description: 定量分析控制层
 * @date 2025/8/5 10:56
 */

@Api(tags = "招商分析控制")
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {
    @Resource
    private AnalysisModel analysisModel;

    /***
     * @description: 接口1 返回页面左上 关键词搜索量 关键词难度
     * @param:
     * @param dto
     * @author sunshihao
     * @date: 2025/8/7 13:46
     */
    @PostMapping("/keyword")
    public JsonResult<KeyWordPartVO> searchVolume(@RequestBody SearchVolumeDTO dto) {
        HashMap<String,String> headers = analysisModel.getCredential();
        KeyWordPartVO result;
        try {
        List<String> keyword = analysisModel.translateProduct(dto.getProductName());
        result = analysisModel.searchVolume(headers, keyword.get(0), dto.getLocationName());
        result.setKeywordEn(keyword.get(0));
        result.setKeywords(keyword);
        }catch (Exception e) {
                log.error(e.getMessage(),e);
            return SldResponse.fail("Analysis failed: " + e.getMessage());
        }
        return SldResponse.success(result);
    }

    @PostMapping("/suggestions")
    public JsonResult<SuggestionVO> suggestions(@RequestBody SearchVolumeDTO dto) {
        HashMap<String,String> headers = analysisModel.getCredential();
        SuggestionVO result;
        try {
            List<String> keyword = analysisModel.translateProduct(dto.getProductName());
            result = analysisModel.KeywordForSuggestions(headers, keyword.get(0), dto.getLocationName());
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return SldResponse.fail("Analysis failed: " + e.getMessage());
        }
        return SldResponse.success(result);
    }



    /***
     * @description: 竞品网站信息
     * @param dto
     * @return: com.slodon.b2b2c.core.response.JsonResult<com.slodon.b2b2c.investment.bean.vo.RivalPartVO>
     * @author sunshihao
     * @date: 2025/8/8 13:23
     */
    @PostMapping("/rival")
    public JsonResult<RivalPartVO> rival(@RequestBody RivalDTO dto) {
        HashMap<String,String> headers = analysisModel.getCredential();
        RivalPartVO result;
        try {
            result = analysisModel.rival(headers, dto);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return SldResponse.fail("Analysis failed: " + e.getMessage());
        }
        return SldResponse.success(result);
    }



    /***
     * @description: 定性分析
     * @param:
     * @param keyword  关键词
     * @return: com.slodon.b2b2c.core.response.JsonResult<java.lang.String>
     * @author sunshihao
     * @date: 2025/8/8 13:30
     */
    @GetMapping("/qualitative")
    public JsonResult<String> qualitative(@RequestParam String keyword) {
        String result;
        try {
            result = analysisModel.qualitative(keyword);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return SldResponse.fail("Analysis failed: " + e.getMessage());
        }
        return SldResponse.success(result);
    }


}
