package com.jeesharp.business.member.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jeesharp.business.modules.member.application.MemberFavoriteApplicationService;
import com.jeesharp.business.modules.member.dto.MemberFavoriteToggleDTO;
import com.jeesharp.business.modules.member.vo.MemberFavoriteListVO;
import com.jeesharp.business.modules.member.vo.MemberFavoriteQueryVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.query.PageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 移动端会员统一收藏接口，支持线路/导览/专题/商品等跨业务收藏。
 *
 * @author jeesharp
 * @since 2026-05-20
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/favorite")
@Tag(name = "移动端-我的收藏", description = "会员跨业务收藏")
@AllArgsConstructor
public class FavoriteMobileController extends BaseMobileController {

    private final MemberFavoriteApplicationService memberFavoriteApplicationService;

    @Operation(summary = "切换收藏")
    @PostMapping("/toggle")
    @SaCheckLogin
    public AjaxResult<Map<String, Object>> toggle(@RequestBody @Valid MemberFavoriteToggleDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        boolean favorited = memberFavoriteApplicationService.toggle(memberId, dto);
        Map<String, Object> data = new HashMap<>(2);
        data.put("favorited", favorited);
        return AjaxResult.ok(favorited ? "已收藏" : "已取消收藏", data);
    }

    @Operation(summary = "取消收藏")
    @PostMapping("/remove")
    @SaCheckLogin
    public AjaxResult<Void> remove(@RequestBody Map<String, String> body) {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberFavoriteApplicationService.remove(memberId, body.get("bizType"), body.get("bizId"));
        return AjaxResult.ok("已取消收藏");
    }

    @Operation(summary = "收藏列表（分页）")
    @PostMapping("/list")
    @SaCheckLogin
    public AjaxResult<PageRespVO<MemberFavoriteListVO>> list(
            @RequestBody(required = false) MemberFavoriteQueryVO query) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberFavoriteApplicationService.findPage(memberId, query));
    }

    @Operation(summary = "收藏全量列表（小程序同步，最多500条）")
    @GetMapping("/all")
    @SaCheckLogin
    public AjaxResult<List<MemberFavoriteListVO>> all(
            @RequestParam(value = "bizType", required = false) String bizType) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberFavoriteApplicationService.listAll(memberId, bizType, 500));
    }

    @Operation(summary = "是否已收藏")
    @GetMapping("/check")
    @SaCheckLogin
    public AjaxResult<Map<String, Object>> check(
            @RequestParam("bizType") String bizType,
            @RequestParam("bizId") String bizId) {
        String memberId = MemberAuthUtils.getLoginUserId();
        boolean favorited = memberFavoriteApplicationService.isFavorite(memberId, bizType, bizId);
        Map<String, Object> data = new HashMap<>(1);
        data.put("favorited", favorited);
        return AjaxResult.okData(data);
    }

    @Operation(summary = "收藏总数")
    @GetMapping("/count")
    @SaCheckLogin
    public AjaxResult<Long> count() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberFavoriteApplicationService.countByMemberId(memberId));
    }
}
