package com.jeesharp.business.modules.marketing.tenantQrcode.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.marketing.tenantQrcode.application.MarketingTenantQrcodeApplicationService;
import com.jeesharp.business.modules.marketing.tenantQrcode.dto.MarketingTenantQrcodeCreateDTO;
import com.jeesharp.business.modules.marketing.tenantQrcode.entity.MarketingTenantQrcode;
import com.jeesharp.business.modules.marketing.tenantQrcode.vo.MarketingTenantQrcodeChannelPresetVO;
import com.jeesharp.business.modules.marketing.tenantQrcode.vo.MarketingTenantQrcodeCreateResultVO;
import com.jeesharp.business.modules.marketing.tenantQrcode.vo.MarketingTenantQrcodeListVO;
import com.jeesharp.business.modules.marketing.tenantQrcode.vo.MarketingTenantQrcodeQueryVO;
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.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;

/**
 * 店铺推广二维码 REST 接口。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/marketing/tenantQrcode/v1")
@Tag(name = "店铺推广二维码", description = "推广码生成与管理")
@AllArgsConstructor
public class MarketingTenantQrcodeController extends BaseController {

    private final MarketingTenantQrcodeApplicationService marketingTenantQrcodeApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('marketing:tenantQrcode:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<MarketingTenantQrcodeListVO>> list(@RequestBody MarketingTenantQrcodeQueryVO query) {
        Page<MarketingTenantQrcode> page = new Page<>(query);
        return AjaxResult.okData(marketingTenantQrcodeApplicationService.findPage(page, query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "推广渠道预设列表")
    @PreAuthorize("@ss.hasPermission('marketing:tenantQrcode:list')")
    @GetMapping("channelPresets")
    public AjaxResult<List<MarketingTenantQrcodeChannelPresetVO>> channelPresets() {
        return AjaxResult.okData(marketingTenantQrcodeApplicationService.listChannelPresets());
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "生成推广码")
    @PreAuthorize("@ss.hasPermission('marketing:tenantQrcode:create')")
    @PostMapping("create")
    public AjaxResult<MarketingTenantQrcodeCreateResultVO> create(@RequestBody @Valid MarketingTenantQrcodeCreateDTO dto) {
        return AjaxResult.okData(marketingTenantQrcodeApplicationService.create(dto));
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "推广码图片（鉴权代理，供管理端预览）")
    @PreAuthorize("@ss.hasPermission('marketing:tenantQrcode:list')")
    @GetMapping("image")
    public void image(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
        marketingTenantQrcodeApplicationService.writeQrcodeImage(id, response);
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('marketing:tenantQrcode:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        marketingTenantQrcodeApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
