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

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.snack.server.common.Result;
import com.snack.server.module.cart.dto.req.CartAddReq;
import com.snack.server.module.cart.dto.req.CartUpdateReq;
import com.snack.server.module.cart.service.CartService;
import com.snack.server.module.cart.vo.CartVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车（用户端）
 */
@Tag(name = "购物车")
@RestController
@RequestMapping("/api/cart")
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;

    @Operation(summary = "获取购物车")
    @SaCheckLogin
    @GetMapping
    public Result<CartVO> list() {
        return Result.ok(cartService.getCurrentCart());
    }

    @Operation(summary = "添加商品到购物车")
    @SaCheckLogin
    @PostMapping
    public Result<Void> add(@Valid @RequestBody CartAddReq req) {
        cartService.addItem(req);
        return Result.ok();
    }

    @Operation(summary = "修改购物车商品数量")
    @SaCheckLogin
    @PutMapping("/quantity")
    public Result<Void> updateQuantity(@Valid @RequestBody CartUpdateReq req) {
        cartService.updateQuantity(req);
        return Result.ok();
    }

    @Operation(summary = "切换选中状态")
    @SaCheckLogin
    @PutMapping("/{skuId}/selected")
    public Result<Void> updateSelected(
            @Parameter(description = "SKU ID") @PathVariable Long skuId,
            @Parameter(description = "0-未选 1-已选") @RequestParam Integer selected) {
        cartService.updateSelected(skuId, selected);
        return Result.ok();
    }

    @Operation(summary = "全选 / 全不选")
    @SaCheckLogin
    @PutMapping("/all-selected")
    public Result<Void> updateAllSelected(
            @Parameter(description = "0-全不选 1-全选") @RequestParam Integer selected) {
        cartService.updateAllSelected(selected);
        return Result.ok();
    }

    @Operation(summary = "删除购物车项")
    @SaCheckLogin
    @DeleteMapping("/{skuId}")
    public Result<Void> delete(@Parameter(description = "SKU ID") @PathVariable Long skuId) {
        cartService.deleteItem(skuId);
        return Result.ok();
    }

    @Operation(summary = "批量删除（结算页）")
    @SaCheckLogin
    @DeleteMapping
    public Result<Void> batchDelete(@RequestBody List<Long> skuIds) {
        cartService.deleteItems(skuIds);
        return Result.ok();
    }

    @Operation(summary = "清空购物车")
    @SaCheckLogin
    @DeleteMapping("/all")
    public Result<Void> clear() {
        cartService.clear();
        return Result.ok();
    }

    @Operation(summary = "购物车商品数量（用于角标）")
    @SaCheckLogin
    @GetMapping("/count")
    public Result<Integer> count() {
        Long userId = cn.dev33.satoken.stp.StpUtil.getLoginIdAsLong();
        return Result.ok(cartService.countByUserId(userId));
    }
}
