package com.snack.server.module.favorite.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.favorite.service.FavoriteService;
import com.snack.server.module.favorite.vo.FavoriteVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收藏（用户端）
 */
@Tag(name = "用户收藏")
@RestController
@RequestMapping("/api/favorite")
@RequiredArgsConstructor
public class FavoriteController {

    private final FavoriteService favoriteService;

    @Operation(summary = "分页获取收藏列表")
    @SaCheckLogin
    @GetMapping
    public Result<Page<FavoriteVO>> list(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页条数", example = "10") @RequestParam(defaultValue = "10") Long size) {
        return Result.ok(favoriteService.pageByCurrentUser(current, size));
    }

    @Operation(summary = "添加收藏")
    @SaCheckLogin
    @PostMapping("/{productId}")
    public Result<Void> add(@Parameter(description = "商品 SPU ID") @PathVariable Long productId) {
        favoriteService.addFavorite(productId);
        return Result.ok();
    }

    @Operation(summary = "取消收藏（按收藏 ID）")
    @SaCheckLogin
    @DeleteMapping("/{favoriteId}")
    public Result<Void> cancel(@Parameter(description = "收藏记录 ID") @PathVariable Long favoriteId) {
        favoriteService.cancelFavorite(favoriteId);
        return Result.ok();
    }

    @Operation(summary = "取消收藏（按商品 ID）")
    @SaCheckLogin
    @DeleteMapping("/product/{productId}")
    public Result<Void> cancelByProductId(@Parameter(description = "商品 SPU ID") @PathVariable Long productId) {
        favoriteService.cancelFavoriteByProductId(productId);
        return Result.ok();
    }

    @Operation(summary = "检查是否已收藏")
    @SaCheckLogin
    @GetMapping("/check")
    public Result<Boolean> check(@Parameter(description = "商品 SPU ID") @RequestParam Long productId) {
        return Result.ok(favoriteService.isFavorited(productId));
    }

    @Operation(summary = "获取收藏数量（用于角标）")
    @SaCheckLogin
    @GetMapping("/count")
    public Result<Long> count() {
        return Result.ok(favoriteService.getFavoriteCount());
    }
}
