package com.backendsys.controller.B2c;

import com.backendsys.aspect.HttpRequestAspect;
import com.backendsys.entity.B2c.B2cShoppingCartDTO;
import com.backendsys.service.B2c.B2cGoodSpecSkuService;
import com.backendsys.service.B2c.B2cShoppingCartService;
import com.backendsys.utils.response.Result;
import com.backendsys.utils.response.ResultEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 商品品牌
 */
@Validated
@RestController
public class B2cShoppingCartController {

    @Autowired
    private HttpRequestAspect httpRequestAspect;

    @Autowired
    private B2cShoppingCartService b2cShoppingCartService;

    @Autowired
    private B2cGoodSpecSkuService b2cGoodSpecSkuService;

    /**
     * 购物车列表 (自己的)
     */
    @GetMapping("/api/front/b2c/shopping/getShoppingCart")
    public Result getShoppingCart() {

        // .. 获取时，需要同时获取商品规格的库存

        return Result.success(b2cShoppingCartService.queryShoppingCartList());
    }

    /**
     * 创建/编辑购物车 (自己的) (member_id, good_id, sku_id 同时且唯一)
     * - 判断 SKU 是否存在
     * - 判断 quantity (购买数量) 是否大于 stock (SKU库存数量)
     * - 判断 商品与SKU 是否一致，是则编辑，否则创建
     */
    @PreAuthorize("@ss.isMember")
    @PostMapping("/api/front/b2c/shopping/createOrUpdateShoppingCart")
    public Result createShoppingCart(@Validated(B2cShoppingCartDTO.Create.class) @RequestBody B2cShoppingCartDTO b2cShoppingCartDTO, HttpServletRequest request) {
        Map<String, Object> response = new LinkedHashMap<>();
        String message = "";

        // [判断] SKU 是否存在
        Map<String, Object> goodSpecSkuDetail = b2cGoodSpecSkuService.queryGoodSpecSkuDetail(b2cShoppingCartDTO.getSku_id());
        if (goodSpecSkuDetail == null) {
            return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "商品规格不存在");
        }

        // [判断] quantity (购买数量) 是否大于 stock (SKU库存数量)
        Long quantity = b2cShoppingCartDTO.getQuantity();
        Integer stock = (Integer) goodSpecSkuDetail.get("stock");
        if (quantity > stock.longValue()) {
            return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "库存不足");
        }

        // [设置] member_id (为自己)
        b2cShoppingCartDTO.setMember_id(httpRequestAspect.getMemberId());

        // [判断] 商品(good_id)与SKU(sku_id) 是否一致，是则编辑，否则创建
        Map<String, Object> shoppingCartDetail = b2cShoppingCartService.queryShoppingCartDetail(b2cShoppingCartDTO);
        if (shoppingCartDetail != null && shoppingCartDetail.get("good_id").equals(b2cShoppingCartDTO.getGood_id()) && shoppingCartDetail.get("sku_id").equals(b2cShoppingCartDTO.getSku_id())) {
            b2cShoppingCartDTO.setShopping_cart_id((Long) shoppingCartDetail.get("id"));
            response = b2cShoppingCartService.updateShoppingCart(b2cShoppingCartDTO);
            message = "更新成功";
        } else {
            response = b2cShoppingCartService.insertShoppingCart(b2cShoppingCartDTO);
            message = "创建成功";
        }

        response.put("quantity", b2cShoppingCartDTO.getQuantity());
        return Result.success(response, message);
    }

    /**
     * 清空购物车 (自己的)
     */
    @PreAuthorize("@ss.isMember")
    @PostMapping("/api/front/b2c/shopping/cleanShoppingCart")
    public Result cleanShoppingCart(HttpServletRequest request) {
        B2cShoppingCartDTO b2cShoppingCartDTO = new B2cShoppingCartDTO();
        b2cShoppingCartDTO.setMember_id(httpRequestAspect.getMemberId());
        return Result.success(b2cShoppingCartService.cleanShoppingCart(b2cShoppingCartDTO), "清空成功");
    }
}
