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

import cn.hutool.core.util.StrUtil;

import com.jeesharp.business.modules.setting.application.HomeQuickGridApplicationService;
import com.jeesharp.business.modules.setting.dto.HomeQuickGridSaveDTO;
import com.jeesharp.business.modules.setting.entity.CmsHomeQuickGrid;
import com.jeesharp.business.modules.setting.vo.HomeQuickGridListVO;
import com.jeesharp.business.modules.setting.vo.HomeQuickGridNavSceneOptionVO;
import com.jeesharp.business.modules.setting.vo.HomeQuickGridQueryVO;
import com.jeesharp.business.modules.setting.vo.HomeQuickGridVO;
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-15
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/homeQuickGrid/v1")
@Tag(name = "站点设置-首页宫格")
@AllArgsConstructor
public class HomeQuickGridController extends BaseController {

    private final HomeQuickGridApplicationService homeQuickGridApplicationService;

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

    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('setting:homeQuickGrid:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<HomeQuickGridListVO>> list(@RequestBody HomeQuickGridQueryVO query) {
        return AjaxResult.okData(homeQuickGridApplicationService.findPage(new Page<CmsHomeQuickGrid>(query), query));
    }

    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('setting:homeQuickGrid:save')")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody @Valid HomeQuickGridSaveDTO dto) {
        homeQuickGridApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

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

    @Operation(summary = "宫格业态场景下拉（注册表 NAV 能力）")
    @PreAuthorize("@ss.hasPermission('setting:homeQuickGrid:info')")
    @GetMapping("navBizSceneOptions")
    public AjaxResult<List<HomeQuickGridNavSceneOptionVO>> navBizSceneOptions() {
        return AjaxResult.okData(homeQuickGridApplicationService.listNavBizSceneOptions());
    }
}
