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.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerChatSession;
import shop.alien.store.service.LawyerChatSessionService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 聊天会话 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-聊天会话"})
@ApiSort(15)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/chatSession")
@RequiredArgsConstructor
public class LawyerChatSessionController {

    private final LawyerChatSessionService chatSessionService;

    @ApiOperation("新增聊天会话")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addChatSession")
    public R<LawyerChatSession> addChatSession(@RequestBody LawyerChatSession chatSession) {
        log.info("LawyerChatSessionController.addChatSession?chatSession={}", chatSession);
        return chatSessionService.addChatSession(chatSession);
    }

    @ApiOperation("编辑聊天会话")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editChatSession")
    public R<LawyerChatSession> editChatSession(@RequestBody LawyerChatSession chatSession) {
        log.info("LawyerChatSessionController.editChatSession?chatSession={}", chatSession);
        return chatSessionService.editChatSession(chatSession);
    }

    @ApiOperation("删除聊天会话")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteChatSession")
    public R<Boolean> deleteChatSession(@RequestParam(value = "id") Integer id) {
        log.info("LawyerChatSessionController.deleteChatSession?id={}", id);
        return chatSessionService.deleteChatSession(id);
    }

    @ApiOperation("保存或更新聊天会话")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerChatSession> saveOrUpdate(@RequestBody LawyerChatSession chatSession) {
        log.info("LawyerChatSessionController.saveOrUpdate?chatSession={}", chatSession);
        boolean result = chatSessionService.saveOrUpdate(chatSession);
        if (result) {
            return R.data(chatSession);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "会话状态", 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<LawyerChatSession>> getList(@ModelAttribute LawyerChatSession chatSession) {
        log.info("LawyerChatSessionController.getList?chatSession={}", chatSession);
        List<LawyerChatSession> list = QueryBuilder.of(chatSession)
                .build()
                .list(chatSessionService);
        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 = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "会话状态", 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<LawyerChatSession>> getPage(@ModelAttribute LawyerChatSession chatSession,
                                              @RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerChatSessionController.getPage?chatSession={},page={},size={}", chatSession, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerChatSession> pageResult = QueryBuilder.of(chatSession)
                .page(pageNum, pageSize)
                .build()
                .page(chatSessionService);
        return R.data(pageResult);
    }
}

