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

import cn.hutool.core.util.StrUtil;

import com.jeesharp.business.modules.common.bizscene.vo.BizSceneOptionVO;
import com.jeesharp.business.modules.setting.application.HomeBannerApplicationService;
import com.jeesharp.business.modules.setting.dto.HomeBannerSaveDTO;
import com.jeesharp.business.modules.setting.entity.CmsHomeBanner;
import com.jeesharp.business.modules.setting.vo.HomeBannerJumpBizOptionVO;
import com.jeesharp.business.modules.setting.vo.HomeBannerListVO;
import com.jeesharp.business.modules.setting.vo.HomeBannerQueryVO;
import com.jeesharp.business.modules.setting.vo.HomeBannerVO;
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 lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页/商城轮播图管理（后台管理端）。
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/homeBanner/v1")
@Tag(name = "站点设置-轮播图")
@AllArgsConstructor
public class HomeBannerController extends BaseController {

    private final HomeBannerApplicationService homeBannerApplicationService;

    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:info')")
    @GetMapping("info")
    public AjaxResult<HomeBannerVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(homeBannerApplicationService.get(id));
    }

    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<HomeBannerListVO>> list(@RequestBody HomeBannerQueryVO query) {
        return AjaxResult.okData(homeBannerApplicationService.findPage(new Page<CmsHomeBanner>(query), query));
    }

    @Operation(summary = "新增")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:create')")
    @PostMapping("create")
    public AjaxResult<Void> create(@RequestBody @Valid HomeBannerSaveDTO dto) {
        homeBannerApplicationService.create(dto);
        return AjaxResult.ok("新增成功");
    }

    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:update')")
    @PostMapping("update")
    public AjaxResult<Void> update(@RequestBody @Valid HomeBannerSaveDTO dto) {
        homeBannerApplicationService.update(dto);
        return AjaxResult.ok("更新成功");
    }

    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        homeBannerApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "跳转目标候选项（远程搜索）")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:info')")
    @GetMapping("jumpBizOptions")
    public AjaxResult<List<HomeBannerJumpBizOptionVO>> jumpBizOptions(
            @RequestParam("bizType") String bizType,
            @RequestParam(value = "keyword", required = false) String keyword) {
        return AjaxResult.okData(homeBannerApplicationService.listJumpBizOptions(bizType, keyword));
    }

    @Operation(summary = "业务场景下拉（注册表 BANNER 能力）")
    @PreAuthorize("@ss.hasPermission('setting:homeBanner:info')")
    @GetMapping("bizSceneOptions")
    public AjaxResult<List<BizSceneOptionVO>> bizSceneOptions() {
        return AjaxResult.okData(homeBannerApplicationService.listBizSceneOptions());
    }
}
