package com.jeesharp.business.modules.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.application.MallBrowseFootprintApplicationService;
import com.jeesharp.business.modules.mall.vo.MallBrowseFootprintListVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 浏览足迹移动端接口：会员 ID 由登录态解析。
 *
 * @author jeesharp
 * @since 2026-06-05
 */
@RestController
@RequestMapping(value = "${jeesharp.web.mobilePath}/mall/browseFootprint/v1")
@Tag(name = "浏览足迹")
@AllArgsConstructor
public class MallBrowseFootprintMobileController {

    private final MallBrowseFootprintApplicationService mallBrowseFootprintApplicationService;

    @Operation(summary = "记录浏览足迹")
    @PostMapping(value = "record")
    public AjaxResult<Void> recordBrowse(@RequestParam("spuId") String spuId) {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            return AjaxResult.fail(201, "请先登录");
        }
        if (StrUtil.isBlank(spuId)) {
            return AjaxResult.failMsg("商品不能为空");
        }
        mallBrowseFootprintApplicationService.recordBrowseFootprint(memberId, spuId.trim());
        return AjaxResult.ok();
    }

    @Operation(summary = "获取用户浏览足迹")
    @GetMapping(value = "list")
    public AjaxResult<PageRespVO<MallBrowseFootprintListVO>> getList(
        @RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
        @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize) {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            PageRespVO<MallBrowseFootprintListVO> empty = new PageRespVO<>();
            empty.setRecords(java.util.Collections.emptyList());
            empty.setTotal(0L);
            empty.setPageNo(pageNo);
            empty.setPageSize(pageSize);
            empty.setPages(0L);
            return AjaxResult.okData(empty);
        }
        PageRespVO<MallBrowseFootprintListVO> page = mallBrowseFootprintApplicationService.getUserBrowseFootprints(
            memberId, new Page<>(pageNo, pageSize));
        return AjaxResult.okData(page);
    }

    @Operation(summary = "删除单条浏览足迹")
    @PostMapping(value = "delete")
    public AjaxResult<Void> deleteFootprint(@RequestParam("spuId") String spuId) {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            return AjaxResult.fail(201, "请先登录");
        }
        if (StrUtil.isBlank(spuId)) {
            return AjaxResult.failMsg("商品不能为空");
        }
        mallBrowseFootprintApplicationService.deleteFootprint(memberId, spuId.trim());
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "清空浏览足迹")
    @PostMapping(value = "clear")
    public AjaxResult<Void> clearFootprints() {
        String memberId = MemberAuthUtils.getLoginUserId();
        if (StrUtil.isBlank(memberId)) {
            return AjaxResult.fail(201, "请先登录");
        }
        mallBrowseFootprintApplicationService.clearUserBrowseFootprints(memberId);
        return AjaxResult.ok("清空成功");
    }
}
