package com.jeesharp.business.modules.coupon.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.coupon.application.CouponTemplateApplicationService;
import com.jeesharp.business.modules.coupon.dto.CouponTemplateClaimQrcodeGenerateDTO;
import com.jeesharp.business.modules.coupon.dto.CouponTemplateSaveDTO;
import com.jeesharp.business.modules.coupon.dto.CouponTemplateStatusDTO;
import com.jeesharp.business.modules.coupon.entity.CouponTemplate;
import com.jeesharp.business.modules.coupon.vo.CouponSceneOptionVO;
import com.jeesharp.business.modules.coupon.vo.CouponTemplateClaimQrcodeVO;
import com.jeesharp.business.modules.coupon.vo.CouponTemplateInfoVO;
import com.jeesharp.business.modules.coupon.vo.CouponTemplateListVO;
import com.jeesharp.business.modules.coupon.vo.CouponTemplateQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 管理端优惠券模板接口，提供模板分页、详情、保存、删除与上下架。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/coupon/couponTemplate/v1")
@Tag(name = "优惠券模板", description = "优惠券模板管理")
@AllArgsConstructor
public class CouponTemplateController extends BaseController {

    private final CouponTemplateApplicationService couponTemplateApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<CouponTemplateListVO>> list(@RequestBody CouponTemplateQueryVO query) {
        Page<CouponTemplate> page = new Page<>(query);
        return AjaxResult.okData(couponTemplateApplicationService.findPage(page, query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:info')")
    @GetMapping("info")
    public AjaxResult<CouponTemplateInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(couponTemplateApplicationService.get(id));
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "新增")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:create') or @ss.hasPermission('coupon:couponTemplate:save')")
    @PostMapping("create")
    public AjaxResult<String> create(@RequestBody @Valid CouponTemplateSaveDTO dto) {
        String id = couponTemplateApplicationService.create(dto);
        return AjaxResult.ok("新增成功", id);
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:update') or @ss.hasPermission('coupon:couponTemplate:save')")
    @PostMapping("update")
    public AjaxResult<Void> update(@RequestBody @Valid CouponTemplateSaveDTO dto) {
        couponTemplateApplicationService.update(dto);
        return AjaxResult.ok("更新成功");
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@Parameter(description = "主键，逗号分隔") @RequestParam("ids") String ids) {
        couponTemplateApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "上下架/状态变更")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:save')")
    @PostMapping("updateStatus")
    public AjaxResult<Void> updateStatus(@RequestBody @Valid CouponTemplateStatusDTO dto) {
        couponTemplateApplicationService.updateStatus(dto);
        return AjaxResult.ok("操作成功");
    }

    @ApiOperationSupport(order = 7)
    @Operation(summary = "使用场景选项（模板配置下拉，来源 cfg_biz_scene）")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:list') or @ss.hasPermission('coupon:couponTemplate:save')")
    @GetMapping("sceneOptions")
    public AjaxResult<List<CouponSceneOptionVO>> sceneOptions() {
        return AjaxResult.okData(couponTemplateApplicationService.listSceneOptions(null));
    }

    @ApiOperationSupport(order = 8)
    @Operation(summary = "生成领券太阳码（线下打印扫码领取）")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:save') or @ss.hasPermission('coupon:couponTemplate:update')")
    @PostMapping("generateClaimQrcode")
    public AjaxResult<CouponTemplateClaimQrcodeVO> generateClaimQrcode(
            @RequestBody @Valid CouponTemplateClaimQrcodeGenerateDTO dto) {
        return AjaxResult.okData(couponTemplateApplicationService.generateClaimQrcode(dto));
    }

    @ApiOperationSupport(order = 9)
    @Operation(summary = "领券太阳码图片（鉴权代理，供管理端预览/下载）")
    @PreAuthorize("@ss.hasPermission('coupon:couponTemplate:list') or @ss.hasPermission('coupon:couponTemplate:info')")
    @GetMapping("claimQrcode/image")
    public void claimQrcodeImage(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
        couponTemplateApplicationService.writeClaimQrcodeImage(id, response);
    }
}
