package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerAiInteractionLog;
import shop.alien.lawyer.service.LawyerAiInteractionLogService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;
import java.util.Map;

/**
 * AI交互日志 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-AI交互日志"})
@ApiSort(21)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/aiInteractionLog")
@RequiredArgsConstructor
public class LawyerAiInteractionLogController {

    private final LawyerAiInteractionLogService aiInteractionLogService;

    @ApiOperation("新增AI交互日志")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addAiInteractionLog")
    public R<LawyerAiInteractionLog> addAiInteractionLog(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
        log.info("LawyerAiInteractionLogController.addAiInteractionLog?aiInteractionLog={}", aiInteractionLog);
        return aiInteractionLogService.addAiInteractionLog(aiInteractionLog);
    }

    @ApiOperation("编辑AI交互日志")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editAiInteractionLog")
    public R<LawyerAiInteractionLog> editAiInteractionLog(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
        log.info("LawyerAiInteractionLogController.editAiInteractionLog?aiInteractionLog={}", aiInteractionLog);
        return aiInteractionLogService.editAiInteractionLog(aiInteractionLog);
    }

    @ApiOperation("删除AI交互日志")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteAiInteractionLog")
    public R<Boolean> deleteAiInteractionLog(@RequestParam(value = "id") Integer id) {
        log.info("LawyerAiInteractionLogController.deleteAiInteractionLog?id={}", id);
        return aiInteractionLogService.deleteAiInteractionLog(id);
    }

    @ApiOperation("保存或更新AI交互日志")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerAiInteractionLog> saveOrUpdate(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
        log.info("LawyerAiInteractionLogController.saveOrUpdate?aiInteractionLog={}", aiInteractionLog);
        boolean result = aiInteractionLogService.saveOrUpdate(aiInteractionLog);
        if (result) {
            return R.data(aiInteractionLog);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "conversationId", value = "会话ID（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getList")
    public R<List<LawyerAiInteractionLog>> getList(@ModelAttribute LawyerAiInteractionLog aiInteractionLog) {
        log.info("LawyerAiInteractionLogController.getList?aiInteractionLog={}", aiInteractionLog);
        List<LawyerAiInteractionLog> list = QueryBuilder.of(aiInteractionLog)
                .likeFields("conversationId")  // 会话ID支持模糊查询
                .build()
                .list(aiInteractionLogService);
        return R.data(list);
    }

    @ApiOperation("通用分页查询")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "conversationId", value = "会话ID（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "problemScenarioId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPage")
    public R<IPage<LawyerAiInteractionLog>> getPage(@ModelAttribute LawyerAiInteractionLog aiInteractionLog,
                                                    @RequestParam(defaultValue = "1") int page,
                                                    @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerAiInteractionLogController.getPage?aiInteractionLog={},page={},size={}", aiInteractionLog, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerAiInteractionLog> pageResult = QueryBuilder.of(aiInteractionLog)
                .likeFields("conversationId")  // 会话ID支持模糊查询
                .page(pageNum, pageSize)
                .build()
                .page(aiInteractionLogService);
        return R.data(pageResult);
    }

    @ApiOperation("AI聊天接口")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "用户发送的消息内容", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "会话ID（AI）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID（可选）", dataType = "Integer", paramType = "query")
    })
    @PostMapping("/chat")
    public R<Map<String, Object>> sendAIMessage(
            @RequestParam String message,
            @RequestParam(required = false) String sessionId,
            @RequestParam(required = false) Integer clientUserId) {
        log.info("LawyerAiInteractionLogController.sendAIMessage?message={},sessionId={},clientUserId={}", message, sessionId, clientUserId);
        return aiInteractionLogService.sendAIMessage(message, sessionId, clientUserId);
    }


    @ApiOperation("AI聊天记录保存")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryText", value = "用户发送的消息内容", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "responseText", value = "AI回复内容", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @PostMapping("/saveChatLog")
    public R<Map<String, Object>> saveChatLog(@RequestBody LawyerAiInteractionLog aiInteractionLog) {
        log.info("LawyerAiInteractionLogController.sendAIMessage?aiInteractionLog={}",aiInteractionLog);
        return aiInteractionLogService.saveChatLog(aiInteractionLog);
    }

    @ApiOperation("删除聊天记录")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "AI交互日志表id", dataType = "Integer", paramType = "query", required = true),
    })
    @PostMapping("/updatedLog")
    public R<Map<String, Object>> updatedLog(
            @RequestParam Integer id) {
        log.info("LawyerAiInteractionLogController.updatedLog?id={}", id);
        return aiInteractionLogService.updatedLog(id);
    }

    @ApiOperation("分页查询对话历史")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true)
//            @ApiImplicitParam(name = "conversationId", value = "会话ID（支持模糊查询）", dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "problemScenarId", value = "法律问题场景ID", dataType = "Integer", paramType = "query"),
//            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getLogList")
    public R<IPage<LawyerAiInteractionLog>> getLogList(
            @RequestParam Integer clientUserId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        log.info("LawyerAiInteractionLogController.getLogList?,page={},size={},clientUserId{}", clientUserId, page, size);

        return aiInteractionLogService.getLogList(clientUserId, page, size);
    }


}

