package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.AiIntelligentAssistant;
import shop.alien.entity.store.StoreCustomerService;
import shop.alien.store.service.StoreCustomerServiceService;

import java.util.List;

/**
 * <p>
 * 智能客服
 * </p>
 *
 * @author ssk
 * @since 2024-12-30
 */
@Api(tags = {"二期-智能客服"})
@Slf4j
@ApiSort(10)
@CrossOrigin
@RestController
@RequestMapping("/storeCustomerService")
@RequiredArgsConstructor
public class StoreCustomerServiceController {

    private final StoreCustomerServiceService storeCustomerServiceService;

    @ApiOperation("通过关键字获取答案")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "question", value = "问题关键字", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getByQuestion")
    public R<StoreCustomerService> getByQuestion(@RequestParam String question) {
        try {
            if (!StringUtils.hasText(question)) {
                return R.fail("问题关键字不能为空");
            }
            StoreCustomerService result = storeCustomerServiceService.getByQuestion(question);
            if (result == null) {
                return R.fail("暂未搜索到相应答案");
            }
            return R.data(result);
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.getByQuestion ERROR, question={}, error={}", question, e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取随机问题")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型(1/商家/2用户)", dataType = "String", paramType = "query", required = true)
    , @ApiImplicitParam(name = "limit", value = "数量", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getRandList")
    public R<List<StoreCustomerService>> getRandList(@RequestParam String type, @RequestParam Integer limit) {
        try {
            if (!StringUtils.hasText(type)) {
                return R.fail("类型不能为空");
            }
            if (limit == null || limit <= 0) {
                return R.fail("数量必须大于0");
            }
            List<StoreCustomerService> result = storeCustomerServiceService.getRandList(type, limit);
            return R.data(result);
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.getRandList ERROR, type={}, limit={}, error={}", type, limit, e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("删除问题")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
            })
    @GetMapping("/delStoreCustomerService")
    public R<String> delStoreCustomerService(@RequestParam String id) {
        try {
            if (!StringUtils.hasText(id)) {
                return R.fail("id不能为空");
            }
            storeCustomerServiceService.delStoreCustomerService(id);
            return R.data("删除成功");
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.delStoreCustomerService ERROR, id={}, error={}", id, e.getMessage(), e);
            return R.fail("删除失败");
        }
    }

    @ApiOperation("中台-问题列表")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "question", value = "问题", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "type", value = "类型(1/商家/2用户)", dataType = "String", paramType = "query", required = false),
    })
    @GetMapping("/getStoreCustomerServicePage")
    public R<IPage<StoreCustomerService>> getStoreCustomerServicePage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                       @RequestParam(value = "size", defaultValue = "10") int size,
                                                       @RequestParam(value = "question", required = false) String question,
                                                       @RequestParam(value = "type", required = false) String type) {
        try {
            if (page < 1) {
                page = 1;
            }
            if (size < 1 || size > 100) {
                size = 10;
            }
            log.info("StoreCustomerServiceController.getStoreCustomerServicePage?page={},size={},question={},type={}", page, size, question, type);
            return R.data(storeCustomerServiceService.getStoreCustomerServicePage(page, size, question, type));
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.getStoreCustomerServicePage ERROR, page={}, size={}, error={}", page, size, e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("中台-保存问题")
    @ApiOperationSupport(order = 5)
    @PostMapping("/saveStoreCustomerService")
    public R<StoreCustomerService> saveStoreCustomerService(@RequestBody StoreCustomerService storeCustomerService) {
        try {
            if (storeCustomerService == null) {
                return R.fail("参数不能为空");
            }
            log.info("StoreCustomerServiceController.saveStoreCustomerService?storeCustomerService={}", storeCustomerService);
            StoreCustomerService saved = storeCustomerServiceService.saveStoreCustomerService(storeCustomerService);
            return R.data(saved);
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.saveStoreCustomerService ERROR, storeCustomerService={}, error={}", storeCustomerService, e.getMessage(), e);
            return R.fail("保存失败");
        }
    }

    @ApiOperation("id查询问题")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<StoreCustomerService> getById(@RequestParam String id) {
        try {
            if (!StringUtils.hasText(id)) {
                return R.fail("id不能为空");
            }
            log.info("StoreCustomerServiceController.getById?id={}", id);
            StoreCustomerService saved = storeCustomerServiceService.getById(id);
            if (saved == null) {
                return R.fail("未找到对应的问题");
            }
            return R.data(saved);
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.getById ERROR, id={}, error={}", id, e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("保存聊天")
    @ApiOperationSupport(order = 7)
    @PostMapping("/saveAiIntelligentAssistant")
    public R<List<AiIntelligentAssistant>> saveAiIntelligentAssistant(@RequestBody List<AiIntelligentAssistant> aiIntelligentAssistants) {
        try {
            if (CollectionUtils.isEmpty(aiIntelligentAssistants)) {
                return R.fail("聊天记录不能为空");
            }
            log.info("StoreCustomerServiceController.saveAiIntelligentAssistant?size={}", aiIntelligentAssistants.size());
            List<AiIntelligentAssistant> saved = storeCustomerServiceService.saveAiIntelligentAssistant(aiIntelligentAssistants);
            return R.data(saved);
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.saveAiIntelligentAssistant ERROR, size={}, error={}", 
                    aiIntelligentAssistants != null ? aiIntelligentAssistants.size() : 0, e.getMessage(), e);
            return R.fail("保存失败");
        }
    }

    @ApiOperation("查询聊天记录")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id)", dataType = "String", paramType = "query", required = true)
            , @ApiImplicitParam(name = "time", value = "时间", dataType = "String", paramType = "query", required = true)
            , @ApiImplicitParam(name = "talkSource", value = "1平台使用/2商户运营", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/selectAiIntelligentAssistant")
    public R<List<AiIntelligentAssistant>> selectAiIntelligentAssistant(@RequestParam String userId, 
                                                                         @RequestParam String time,
                                                                         @RequestParam Integer talkSource) {
        try {
            if (!StringUtils.hasText(userId)) {
                return R.fail("用户id不能为空");
            }
            if (!StringUtils.hasText(time)) {
                return R.fail("时间参数不能为空");
            }
            if (talkSource == null || (talkSource != 1 && talkSource != 2)) {
                return R.fail("talkSource参数错误，必须为1或2");
            }
            List<AiIntelligentAssistant> result = storeCustomerServiceService.selectAiIntelligentAssistant(userId, time, talkSource);
            return R.data(result);
        } catch (Exception e) {
            log.error("StoreCustomerServiceController.selectAiIntelligentAssistant ERROR, userId={}, time={}, talkSource={}, error={}", 
                    userId, time, talkSource, e.getMessage(), e);
            return R.fail("查询失败");
        }
    }
}
