package shop.alien.store.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeDiscountCouponFriendRule;
import shop.alien.entity.store.LifeGroupBuyThali;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.entity.store.dto.LifeDiscountCouponStoreFriendDto;
import shop.alien.entity.store.dto.LifeGroupBuyDto;
import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleDetailVo;
import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleVo;
import shop.alien.entity.store.vo.LifeDiscountCouponStoreFriendVo;
import shop.alien.entity.store.vo.LifeGroupBuyCountDateVo;
import shop.alien.store.annotation.TrackEvent;
import shop.alien.store.service.LifeDiscountCouponStoreFriendService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券商户发放好友优惠券关系表	 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-02-19
 */
@Api(tags = {"二期-优惠券商户发放好友优惠券关系表"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/life-discount-coupon-store-friend")
@RequiredArgsConstructor
public class LifeDiscountCouponStoreFriendController {

    private final LifeDiscountCouponStoreFriendService lifeDiscountCouponStoreFriendService;

    @ApiOperation("获取该店铺的好友及其可用优惠券列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getFriendCouponList")
    public R<List<LifeDiscountCouponStoreFriendVo>> getFriendCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestParam(value = "friendUserId", required = false) String friendUserId) {
        log.info("LifeDiscountCouponStoreFriendController.getFriendCouponList");
        try {
            return R.data(lifeDiscountCouponStoreFriendService.getFriendCouponList(userLoginInfo, friendUserId));
        } catch (Exception e) {
            log.error("LifeDiscountCouponStoreFriendController.getFriendCouponList ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

//    @TrackEvent(
//            eventType = "COUPON_GIVE",
//            eventCategory = "COUPON",
//            storeId = "#{#lifeDiscountCouponStoreFriendDto.storeId}",
//            targetType = "COUPON"
//    )
    @ApiOperation("给好友发放优惠券")
    @ApiOperationSupport(order = 2)
    @PostMapping("/setFriendCoupon")
    public R<Boolean> setFriendCoupon(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto) {
        log.info("LifeDiscountCouponStoreFriendController.setFriendCoupon?lifeDiscountCouponStoreFriendDto={}", lifeDiscountCouponStoreFriendDto.toString());
        try {
            boolean save = lifeDiscountCouponStoreFriendService.setFriendCoupon(userLoginInfo, lifeDiscountCouponStoreFriendDto);
            if (!save) {
                return R.fail("设置失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponStoreFriendController.setFriendCoupon ERROR Msg={}", e.getMessage());
            return R.fail("设置失败");
        }
        return R.success("设置成功");
    }

    @ApiOperation("消费后发放好友优惠券并返回列表")
    @ApiOperationSupport(order = 2)
    @PostMapping("/issueFriendCoupon")
    public R<List<LifeDiscountCouponStoreFriendVo>> issueFriendCoupon(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto) {
        log.info("LifeDiscountCouponStoreFriendController.issueFriendCoupon?lifeDiscountCoupon={}", lifeDiscountCouponStoreFriendDto.toString());
        try {
            List<LifeDiscountCouponStoreFriendVo> lifeDiscountCouponStoreFriendVos =
                    lifeDiscountCouponStoreFriendService.issueFriendCoupon(lifeDiscountCouponStoreFriendDto);
            return R.data(lifeDiscountCouponStoreFriendVos);
        } catch (Exception e) {
            log.error("LifeDiscountCouponStoreFriendController.issueFriendCoupon ERROR Msg={}", e.getMessage());
            return R.fail("发放失败");
        }
    }

    @ApiOperation("查询该订单发放优惠券列表")
    @ApiOperationSupport(order = 2)
    @PostMapping("/getSendCoupons")
    public R<List<LifeDiscountCouponStoreFriendVo>> getSendCoupons(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto) {
        log.info("LifeDiscountCouponStoreFriendController.getSendCoupons?lifeDiscountCouponStoreFriendDto={}", lifeDiscountCouponStoreFriendDto.toString());
        try {
            List<LifeDiscountCouponStoreFriendVo> lifeDiscountCouponStoreFriendVos =
                    lifeDiscountCouponStoreFriendService.getSendCoupons(lifeDiscountCouponStoreFriendDto);
            return R.data(lifeDiscountCouponStoreFriendVos);
        } catch (Exception e) {
            log.error("LifeDiscountCouponStoreFriendController.getSendCoupons ERROR Msg={}", e.getMessage());
            return R.fail("发放失败");
        }
    }

    @ApiOperation("发布/取消发布好友商家优惠券")
    @ApiOperationSupport(order = 2)
    @PostMapping("/releaseAndCancelFriendStoreCoupon")
    public R<Boolean> publish(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody Map<String, String> friendStoreMap) {
        log.info("LifeDiscountCouponStoreFriendController.publish?friendStoreMap={}", JSONObject.toJSONString(friendStoreMap));
        int num = lifeDiscountCouponStoreFriendService.releaseAndCancelFriendStoreCoupon(friendStoreMap.get("giveCouponId"), friendStoreMap.get("releaseType"));
        if (num == 0) {
            return R.fail("操作失败");
        }
        return R.success("操作成功");
    }


    @ApiOperation("删除好友优惠券")
    @ApiOperationSupport(order = 2)
    @DeleteMapping("/deleteLifeDiscountCouponStoreFriend/{id}")
    public R<Boolean> deleteDiscountCoupon(@PathVariable("id") Integer id) {
        log.info("LifeDiscountCouponStoreFriendController.deleteLifeDiscountCouponStoreFriend?id={}", id);
        if (lifeDiscountCouponStoreFriendService.removeById(id)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("保存好友赠券规则")
    @PostMapping("/saveFriendCouponRule")
    public R saveFriendCouponRule(@RequestBody LifeDiscountCouponFriendRule lifeDiscountCouponFriendRule) {
        log.info("LifeDiscountCouponStoreFriendController.saveFriendCouponRule?lifeDiscountCouponFriendRule={}", lifeDiscountCouponFriendRule.toString());
        LifeDiscountCouponFriendRuleVo lifeDiscountCouponFriendRuleVo = lifeDiscountCouponStoreFriendService.saveFriendCouponRule(lifeDiscountCouponFriendRule);
        return R.data(lifeDiscountCouponFriendRuleVo);
    }

    @ApiOperation("删除赠券规则")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/delFriendCouponRule")
    private R delFriendCouponRule(@RequestParam(value = "id") String id) {
        log.info("LifeDiscountCouponStoreFriendController.delFriendCouponRule?id={}", id);
        lifeDiscountCouponStoreFriendService.delFriendCouponRule(id);
        return R.success("删除成功");
    }

    @ApiOperation("查询赠券规则明细")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getRuleById")
    private R<LifeDiscountCouponFriendRuleVo> getRuleById(@RequestParam(value = "id") String id) {
        log.info("LifeDiscountCouponStoreFriendController.getRuleById?id={}", id);
        return R.data(lifeDiscountCouponStoreFriendService.getRuleById(id));
    }

    @ApiOperation("查询好友赠券")
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "当前登录店铺id", dataType = "String", paramType = "query", required = true)
    ,@ApiImplicitParam(name = "friendStoreUserId", value = "选中好友店铺用户id", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getReceivedFriendCouponList")
    private R<List<LifeDiscountCouponFriendRuleDetailVo>> getReceivedFriendCouponList(@RequestParam(value = "storeId") String storeId, @RequestParam(value = "friendStoreUserId",required = false)String friendStoreUserId) {
        log.info("LifeDiscountCouponStoreFriendController.getReceivedFriendCouponList?storeId={},friendStoreUserId={}", storeId,friendStoreUserId);
        return R.data(lifeDiscountCouponStoreFriendService.getReceivedFriendCouponList(storeId,friendStoreUserId));
    }

    @ApiOperation("查询赠券规则")
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "当前登录店铺id", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getRuleList")
    private R<List<LifeDiscountCouponFriendRuleVo>> getRuleList(@RequestParam(value = "storeId") String storeId, String acName, String status) {
        log.info("LifeDiscountCouponStoreFriendController.getRuleList?storeId={},name={},status={}", storeId, acName, status);
        return R.data(lifeDiscountCouponStoreFriendService.getRuleList(storeId, acName, status));
    }

    @ApiOperation("查询赠券记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "storeUserId", value = "好友赠我-当前登录店铺id", dataType = "String", paramType = "query", required = false)
            ,@ApiImplicitParam(name = "friendStoreUserId", value = "我赠好友-选中好友店铺用户id", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getReceivedSendFriendCouponList")
    private R<List<LifeDiscountCouponFriendRuleVo>> getReceivedSendFriendCouponList(@RequestParam(value = "storeUserId",required = false) String storeUserId, @RequestParam(value = "friendStoreUserId",required = false)String friendStoreUserId, @RequestParam(value = "storeName",required = false)String storeName) {
        log.info("LifeDiscountCouponStoreFriendController.getReceivedSendFriendCouponList?storeId={},friendStoreUserId={},storeName={}", storeUserId,friendStoreUserId,storeName);
        return R.data(lifeDiscountCouponStoreFriendService.getReceivedSendFriendCouponList(storeUserId,friendStoreUserId,storeName));





    }
}
