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.AppSplashAdApplicationService;
import com.jeesharp.business.modules.setting.dto.AppSplashAdSaveDTO;
import com.jeesharp.business.modules.setting.entity.AppSplashAd;
import com.jeesharp.business.modules.setting.vo.AppSplashAdListVO;
import com.jeesharp.business.modules.setting.vo.AppSplashAdQueryVO;
import com.jeesharp.business.modules.setting.vo.AppSplashAdVO;
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.*;

/**
 * 开屏广告管理（后台），配置小程序/APP 启动全屏图或视频。
 *
 * @author jeesharp
 * @since 2026-04-21
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/appSplashAd/v1")
@Tag(name = "开屏广告管理", description = "用户端启动全屏图/视频配置")
@AllArgsConstructor
public class AppSplashAdController extends BaseController {

    private final AppSplashAdApplicationService appSplashAdApplicationService;

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

    @ApiOperationSupport(order = 2)
    @Operation(summary = "2、开屏广告分页列表", description = "分页查询，支持按端、状态筛选")
    @PreAuthorize("@ss.hasPermission('setting:appSplashAd:list')")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<AppSplashAdListVO>> list(@RequestBody AppSplashAdQueryVO query) {
        PageRespVO<AppSplashAdListVO> page = appSplashAdApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "3、保存开屏广告", description = "新增或更新，ID 为空为新增")
    @PreAuthorize("@ss.hasPermission('setting:appSplashAd:save')")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid AppSplashAdSaveDTO dto) {
        appSplashAdApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

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