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

import cn.hutool.core.util.StrUtil;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.setting.application.OperationPopupApplicationService;
import com.jeesharp.business.modules.setting.dto.OperationPopupSaveDTO;
import com.jeesharp.business.modules.setting.vo.OperationPopupListVO;
import com.jeesharp.business.modules.setting.vo.OperationPopupQueryVO;
import com.jeesharp.business.modules.setting.vo.OperationPopupVO;
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.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 首页运营弹窗管理（后台），配置小程序等端首页弹窗素材与投放时段。
 *
 * @author jeesharp
 * @since 2026-05-13
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/operationPopup/v1")
@Tag(name = "运营弹窗管理", description = "首页运营弹窗配置")
@AllArgsConstructor
public class OperationPopupController extends BaseController {

    private final OperationPopupApplicationService operationPopupApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "1、运营弹窗详情", description = "根据 ID 获取配置")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "403", description = "无权限访问")
    })
    @PreAuthorize("@ss.hasPermission('setting:operationPopup:info')")
    @GetMapping(value = "info")
    public AjaxResult<OperationPopupVO> info(
            @Parameter(description = "ID", required = true) @RequestParam("id") String id) {
        return AjaxResult.okData(operationPopupApplicationService.get(id));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "2、运营弹窗分页列表", description = "分页查询，支持按端、状态筛选")
    @PreAuthorize("@ss.hasPermission('setting:operationPopup:list')")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<OperationPopupListVO>> list(@RequestBody OperationPopupQueryVO query) {
        PageRespVO<OperationPopupListVO> page =
                operationPopupApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "3、保存运营弹窗", description = "新增或更新，ID 为空为新增")
    @PreAuthorize("@ss.hasPermission('setting:operationPopup:save')")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid OperationPopupSaveDTO dto) {
        operationPopupApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "4、删除运营弹窗", description = "批量删除，多个 ID 逗号分隔")
    @PreAuthorize("@ss.hasPermission('setting:operationPopup:delete')")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(
            @Parameter(description = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids) {
        operationPopupApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
