/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.yshop.module.product.controller.app.relation;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.product.controller.app.product.param.AppStoreProductQueryParam;
import co.yixiang.yshop.module.product.controller.app.product.vo.AppIndexVo;
import co.yixiang.yshop.module.product.controller.app.product.vo.AppProductVo;
import co.yixiang.yshop.module.product.controller.app.product.vo.AppStoreProductRespVo;
import co.yixiang.yshop.module.product.controller.app.relation.param.AppStoreProductRelationQueryParam;
import co.yixiang.yshop.module.product.controller.app.relation.vo.AppStoreProductRelationQueryVo;
import co.yixiang.yshop.module.product.enums.product.ProductEnum;
import co.yixiang.yshop.module.product.service.storeproduct.AppStoreProductService;
import co.yixiang.yshop.module.product.service.storeproductrelation.AppStoreProductRelationService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.PARAM_ERROR;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.STORE_PRODUCT_RELATION_EXISTS;

/**
 * <p>
 * 收藏/足迹控制器
 * </p>
 *
 * @author hupeng
 * @since 2023-6-14
 */
@Slf4j
@RestController
@Tag(name = "用户 APP - 收藏(足迹)")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/relation")
public class AppStoreProductRelationController {

    private final AppStoreProductRelationService appStoreProductRelationService;


    /**
     * 添加收藏
     */
    @PostMapping("/collect/add")
    @Operation(summary = "添加收藏")
    @PreAuthenticated
    public CommonResult<Boolean> collectAdd(@Validated @RequestBody AppStoreProductRelationQueryParam param){
        long uid = getLoginUserId();
        if(!NumberUtil.isNumber(param.getId())) {
            throw exception(PARAM_ERROR);
        }
        appStoreProductRelationService.addRroductRelation(Long.valueOf(param.getId()),uid,param.getCategory());
        return success(true);
    }

    /**
     * 取消收藏
     */
    @PreAuthenticated
    @PostMapping("/collect/del")
    @Operation(summary = "取消收藏")
    public CommonResult<Boolean> collectDel(@Validated @RequestBody AppStoreProductRelationQueryParam param){
        long uid = getLoginUserId();
        if(!NumberUtil.isNumber(param.getId())) {
            throw exception(PARAM_ERROR);
        }
        appStoreProductRelationService.delRroductRelation(Long.valueOf(param.getId()),
                uid,param.getCategory());
        return success(true);
    }

    /**
     * 获取收藏产品
     */
    @PreAuthenticated
    @GetMapping("/collect/user")
    @Operation(summary = "获取收藏产品,或足迹")
    @Parameter(name = "type", description = "类型 collect 收藏 root足迹", required = true)
    public CommonResult<List<AppStoreProductRelationQueryVo>> collectUser(@RequestParam(value = "page",defaultValue = "1") int page,
                                                                       @RequestParam(value = "limit",defaultValue = "10") int limit,
                                                                       @RequestParam(value = "type") String type){
        long uid = getLoginUserId();
        return success(appStoreProductRelationService.userCollectProduct(page,limit,uid,type));
    }


}

