package com.ruoyi.web.modules.cart.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.cart.dto.CartCheckoutPrepareDTO;
import com.ruoyi.web.modules.cart.dto.CartItemAddDTO;
import com.ruoyi.web.modules.cart.dto.CartItemBatchDeleteDTO;
import com.ruoyi.web.modules.cart.dto.CartItemCheckedBatchDTO;
import com.ruoyi.web.modules.cart.dto.CartItemQuantityDTO;
import com.ruoyi.web.modules.cart.service.ICartAppService;

/**
 * C 端购物车（须会员 Token）
 */
@RestController
@RequestMapping("/api/cart")
public class CartAppController extends BaseController
{
    @Autowired
    private ICartAppService cartAppService;

    @GetMapping
    public AjaxResult list()
    {
        return success(cartAppService.list(requireMemberId()));
    }

    @PostMapping("/items")
    public AjaxResult addItem(@RequestBody CartItemAddDTO dto)
    {
        return success(cartAppService.addItem(requireMemberId(), dto));
    }

    @PutMapping("/items/checked")
    public AjaxResult updateChecked(@RequestBody CartItemCheckedBatchDTO dto)
    {
        cartAppService.updateChecked(requireMemberId(), dto);
        return success();
    }

    @PutMapping("/items/{cartItemId}/quantity")
    public AjaxResult updateQuantity(@PathVariable("cartItemId") Long cartItemId,
            @RequestBody CartItemQuantityDTO dto)
    {
        return success(cartAppService.updateQuantity(requireMemberId(), cartItemId, dto));
    }

    @DeleteMapping("/items/{cartItemId}")
    public AjaxResult removeItem(@PathVariable("cartItemId") Long cartItemId)
    {
        cartAppService.removeItem(requireMemberId(), cartItemId);
        return success();
    }

    @DeleteMapping("/items")
    public AjaxResult removeItems(@RequestBody CartItemBatchDeleteDTO dto)
    {
        if (dto != null && dto.getCartItemIds() != null)
        {
            cartAppService.removeItems(requireMemberId(), dto.getCartItemIds());
        }
        return success();
    }

    @DeleteMapping("/invalid")
    public AjaxResult cleanInvalid()
    {
        return success(cartAppService.cleanInvalid(requireMemberId()));
    }

    @PostMapping("/checkout/prepare")
    public AjaxResult prepareCheckout(@RequestBody CartCheckoutPrepareDTO dto)
    {
        return success(cartAppService.prepareCheckout(requireMemberId(), dto));
    }

    private Long requireMemberId()
    {
        Long memberId = MemberContext.getMemberId();
        if (memberId == null)
        {
            throw new ServiceException("请先登录", 401);
        }
        return memberId;
    }
}
