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.LawyerUserSearchHistory;
import shop.alien.lawyer.service.LawyerUserSearchHistoryService;


import java.util.List;

/**
 * 用户搜索历史 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-用户搜索历史"})
@ApiSort(22)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/userSearchHistory")
@RequiredArgsConstructor
public class LawyerUserSearchHistoryController {

    private final LawyerUserSearchHistoryService userSearchHistoryService;

    @ApiOperation("新增用户搜索历史（自动限制每个用户最多10条记录，超过则删除最老的记录）")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addUserSearchHistory")
    public R<LawyerUserSearchHistory> addUserSearchHistory(@RequestBody LawyerUserSearchHistory userSearchHistory) {
        log.info("LawyerUserSearchHistoryController.addUserSearchHistory?userSearchHistory={}", userSearchHistory);
        return userSearchHistoryService.addUserSearchHistory(userSearchHistory);
    }

    @ApiOperation("删除用户搜索历史")
    @ApiOperationSupport(order = 2)
    @DeleteMapping("/deleteUserSearchHistory")
    public R<Boolean> deleteUserSearchHistory(@RequestParam(value = "id") Integer id) {
        log.info("LawyerUserSearchHistoryController.deleteUserSearchHistory?id={}", id);
        return userSearchHistoryService.deleteUserSearchHistory(id);
    }

    @ApiOperation("清空用户搜索历史")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/clearUserSearchHistory")
    public R<Boolean> clearUserSearchHistory(@RequestParam(value = "clientUserId") Integer clientUserId) {
        log.info("LawyerUserSearchHistoryController.clearUserSearchHistory?clientUserId={}", clientUserId);
        return userSearchHistoryService.clearUserSearchHistory(clientUserId);
    }

    @ApiOperation("通用列表查询（按创建时间降序，新的在前）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "searchType", 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<LawyerUserSearchHistory>> getList(@ModelAttribute LawyerUserSearchHistory userSearchHistory) {
        log.info("LawyerUserSearchHistoryController.getList?userSearchHistory={}", userSearchHistory);
        List<LawyerUserSearchHistory> list = userSearchHistoryService.getListWithOrder(userSearchHistory);
        return R.data(list);
    }

    @ApiOperation("通用分页查询（按创建时间降序，新的在前）")
    @ApiOperationSupport(order = 5)
    @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 = "searchType", 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<LawyerUserSearchHistory>> getPage(@ModelAttribute LawyerUserSearchHistory userSearchHistory,
                                                     @RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerUserSearchHistoryController.getPage?userSearchHistory={},page={},size={}", userSearchHistory, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerUserSearchHistory> pageResult = userSearchHistoryService.getPageWithOrder(userSearchHistory, pageNum, pageSize);
        return R.data(pageResult);
    }
}

