package com.slodon.b2b2c.investment.controller;

import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.investment.bean.analysis.dto.*;
import com.slodon.b2b2c.investment.bean.analysis.vo.KeyWordPartVO;
import com.slodon.b2b2c.investment.bean.analysis.vo.RivalPartVO;
import com.slodon.b2b2c.investment.bean.analysis.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 org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
import javax.validation.Valid;
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;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /***
     * @description: 接口1 返回页面左上 关键词搜索量 关键词难度
     * @param:
     * @param dto
     * @author sunshihao
     * @date: 2025/8/7 13:46
     */
    @PostMapping("/keyword")
    public JsonResult<KeyWordPartVO> searchVolume(@Valid @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(@Valid @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(@Valid @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);
    }

    /***
     * @description: ai商情客户留存信息记录
     * @param:
     * @param dto 参数
     * @return: void
     * @author sunshihao
     * @date: 2025/9/1 9:45
     */
    @PostMapping("/record")
    public JsonResult<Integer> record(@Valid @RequestBody CompanyDTO dto) {
        int id;
        try {
            String verifySms = stringRedisTemplate.opsForValue().get(dto.getPhone());
            AssertUtil.isTrue(StringUtil.isEmpty(verifySms), "请先获取验证码");
            AssertUtil.isTrue(!dto.getVerifyCode().equals(verifySms), "验证码输入错误，请重试");
            stringRedisTemplate.delete(dto.getPhone()); //删除验证码
            id = analysisModel.record(dto);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return SldResponse.fail(e.getMessage());
        }
        return SldResponse.success(id);
    }

    /***
     * @description: 更新pdf地址
     * @param:
     * @param dto 参数
     * @return: com.slodon.b2b2c.core.response.JsonResult<java.lang.String>
     * @author sunshihao
     * @date: 2025/9/1 9:46
     */
    @PostMapping("/savePdf")
    public JsonResult<String> savePdf(@Valid @RequestBody PdfDTO dto) {
        try {
            analysisModel.savePdf(dto);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return SldResponse.fail( e.getMessage());
        }
        return SldResponse.success();
    }



}
