package shop.alien.storeplatform.controller;

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.UserLoginInfo;
import shop.alien.entity.store.dto.LifeDiscountCouponDto;
import shop.alien.entity.store.vo.LifeDiscountCouponVo;
import shop.alien.storeplatform.service.LifeDiscountCouponPlatformService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 优惠券管理控制器（平台端）
 * 提供优惠券相关的管理接口，包括优惠券的创建、更新、删除、查询、状态切换等功能
 * 支持优惠券库存管理、状态管理、规则配置等操作
 *
 * @author ssk
 * @date 2025-02-07
 */
@Api(tags = {"商家端-优惠券"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/discountCouponPlatform")
@RequiredArgsConstructor
public class LifeDiscountCouponPlatformController {

    private final LifeDiscountCouponPlatformService lifeDiscountCouponPlatformService;

    /**
     * 发布优惠券
     * 创建新的优惠券，包括基本信息、规则配置、时间段限制等
     *
     * @param userLoginInfo 当前登录用户信息
     * @param lifeDiscountCouponDto 优惠券信息对象
     * @return R<Boolean> 操作结果
     * @author ssk
     * @date 2025-02-07
     */
    @ApiOperation("发布优惠券")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addDiscountCoupon")
    public R<Boolean> addDiscountCoupon(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponDto lifeDiscountCouponDto) {
        log.info("LifeDiscountCouponPlatformController.addDiscountCoupon?lifeDiscountCoupon={}", lifeDiscountCouponDto.toString());
        try {
            boolean save = lifeDiscountCouponPlatformService.addDiscountCoupon(lifeDiscountCouponDto);
            if (!save) {
                return R.fail("发布失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponPlatformController.addDiscountCoupon ERROR Msg={}", e.getMessage());
            return R.fail("发布失败");
        }
        return R.success("发布成功");
    }

    /**
     * 修改优惠券信息
     * 更新已存在的优惠券信息，包括基本信息、规则配置、时间段限制等
     *
     * @param lifeDiscountCouponDto 优惠券信息对象
     * @return R<Boolean> 操作结果
     * @author ssk
     * @date 2025-02-07
     */
    @ApiOperation("修改优惠券信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editDiscountCoupon")
    public R<Boolean> editDiscountCoupon(@RequestBody LifeDiscountCouponDto lifeDiscountCouponDto) {
        log.info("LifeDiscountCouponPlatformController.editDiscountCoupon?lifeDiscountCoupon={}", lifeDiscountCouponDto.toString());
        try {
            boolean edited = lifeDiscountCouponPlatformService.editDiscountCoupon(lifeDiscountCouponDto);
            if (!edited) {
                return R.fail("修改失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponPlatformController.editDiscountCoupon ERROR Msg={}", e.getMessage());
            return R.fail("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 删除优惠券信息
     * 根据优惠券ID删除优惠券及其关联的规则信息
     *
     * @param id 优惠券ID
     * @return R<Boolean> 操作结果
     * @author ssk
     * @date 2025-02-07
     */
    @ApiOperation("删除优惠券信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("/deleteDiscountCoupon")
    public R<Boolean> deleteDiscountCoupon(@RequestParam(value = "id") Integer id) {
        log.info("LifeDiscountCouponPlatformController.deleteDiscountCoupon?id={}", id);
        try {
            boolean deleted = lifeDiscountCouponPlatformService.deleteDiscountCoupon(id);
            if (!deleted) {
                return R.fail("删除失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponPlatformController.deleteDiscountCoupon ERROR Msg={}", e.getMessage());
            return R.fail("删除失败");
        }
        return R.success("删除成功");
    }

    /**
     * 切换优惠券进行状态
     * 切换优惠券的可领取状态（启用/暂停）
     *
     * @param counponId 优惠券ID
     * @return R<Boolean> 操作结果
     * @author ssk
     * @date 2025-02-07
     */
    @ApiOperation("切换优惠券进行状态")
    @ApiOperationSupport(order = 4)
    @GetMapping("/cutDiscountCouponStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "counponId", value = "优惠券id", dataType = "String", paramType = "query", required = true)
    })
    public R<Boolean> cutDiscountCouponStatus(@RequestParam("counponId") String counponId) {
        log.info("LifeDiscountCouponPlatformController.cutDiscountCouponStatus?counponId={}", counponId);
        try {
            boolean save = lifeDiscountCouponPlatformService.cutDiscountCouponStatus(counponId);
            if (!save) {
                return R.fail("切换失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponPlatformController.cutDiscountCouponStatus ERROR Msg={}", e.getMessage());
            return R.fail("切换失败");
        }
        return R.success("切换成功");
    }

    /**
     * 获取该优惠券的明细信息
     * 根据优惠券ID查询优惠券的详细信息，包括规则配置、时间段限制等
     *
     * @param userLoginInfo 当前登录用户信息
     * @param counponId 优惠券ID
     * @return R<LifeDiscountCouponVo> 优惠券详情
     * @author ssk
     * @date 2025-02-07
     */
    @ApiOperation("获取该优惠券的明细信息")
    @ApiOperationSupport(order = 5)
    @GetMapping("/getCounponDetailById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "counponId", value = "优惠券id", dataType = "String", paramType = "query", required = true)
    })
    public R<LifeDiscountCouponVo> getCounponDetailById(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                        @RequestParam(value = "counponId") String counponId) {
        log.info("LifeDiscountCouponPlatformController.getCounponDetailById?counponId={}", counponId);
        try {
            LifeDiscountCouponVo counponDetail = lifeDiscountCouponPlatformService.getCounponDetailById(counponId, userLoginInfo);
            return R.data(counponDetail);
        } catch (Exception e) {
            log.error("LifeDiscountCouponPlatformController.getCounponDetailById ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    /**
     * 修改优惠券库存
     * 更新指定优惠券的库存数量
     *
     * @param id 优惠券ID
     * @param singleQty 库存数量
     * @return R<Boolean> 操作结果
     * @author ssk
     * @date 2025-02-07
     */
    @ApiOperation("修改优惠券库存")
    @ApiOperationSupport(order = 2)
    @GetMapping("/updateCouponSingleQty")
    public R<Boolean> updateCouponSingleQty(@RequestParam(value = "id") Integer id, @RequestParam(value = "singleQty") Integer singleQty) {
        log.info("LifeDiscountCouponPlatformController.editDiscountCoupon?id={}, singleQty={}", id, singleQty);
        try {
            boolean edited = lifeDiscountCouponPlatformService.updateCouponSingleQty(id, singleQty);
            if (!edited) {
                return R.fail("修改失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponPlatformController.editDiscountCoupon ERROR Msg={}", e.getMessage());
            return R.fail("修改失败");
        }
        return R.success("修改成功");
    }

}

