package com.jeesharp.business.modules.common.bizscene.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.common.bizscene.application.BizSceneApplicationService;
import com.jeesharp.business.modules.common.bizscene.dto.BizSceneSaveDTO;
import com.jeesharp.business.modules.common.bizscene.entity.BizScene;
import com.jeesharp.business.modules.common.bizscene.vo.BizSceneInfoVO;
import com.jeesharp.business.modules.common.bizscene.vo.BizSceneListVO;
import com.jeesharp.business.modules.common.bizscene.vo.BizSceneOptionVO;
import com.jeesharp.business.modules.common.bizscene.vo.BizSceneQueryVO;
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 lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端业务业态场景注册表 REST 接口。
 *
 * @author jeesharp
 * @since 2026-07-03
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/common/bizScene/v1")
@Tag(name = "业务业态场景", description = "全站业务场景注册与选品元数据配置")
@AllArgsConstructor
public class BizSceneController extends BaseController {

    private final BizSceneApplicationService bizSceneApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:common:bizScene:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<BizSceneListVO>> list(@RequestBody BizSceneQueryVO query) {
        return AjaxResult.okData(bizSceneApplicationService.findPage(new Page<>(query), query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    @PreAuthorize("@ss.hasPermission('manager:business:common:bizScene:info')")
    @GetMapping("info")
    public AjaxResult<BizSceneInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(bizSceneApplicationService.get(id));
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "新增")
    @PreAuthorize("@ss.hasPermission('manager:business:common:bizScene:create')")
    @PostMapping("create")
    public AjaxResult<Void> create(@RequestBody @Valid BizSceneSaveDTO dto) {
        bizSceneApplicationService.create(dto);
        return AjaxResult.ok("新增成功");
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasPermission('manager:business:common:bizScene:update')")
    @PostMapping("update")
    public AjaxResult<Void> update(@RequestBody @Valid BizSceneSaveDTO dto) {
        bizSceneApplicationService.update(dto);
        return AjaxResult.ok("更新成功");
    }

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

    @ApiOperationSupport(order = 6)
    @Operation(summary = "启用场景选项（按能力标签筛选）")
    @PreAuthorize("@ss.hasPermission('manager:business:common:bizScene:list')")
    @GetMapping("enabledOptions")
    public AjaxResult<List<BizSceneOptionVO>> enabledOptions(
            @Parameter(description = "能力标签：COUPON、BANNER、NAV") @RequestParam(value = "capability", required = false) String capability) {
        return AjaxResult.okData(bizSceneApplicationService.listEnabledOptions(capability, null));
    }
}
