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

import com.jeesharp.business.modules.food.application.FoodPackageApplicationService;
import com.jeesharp.business.modules.food.dto.FoodPackageSaveDTO;
import com.jeesharp.business.modules.food.entity.FoodPackage;
import com.jeesharp.business.modules.food.vo.FoodPackageInfoVO;
import com.jeesharp.business.modules.food.vo.FoodPackageListVO;
import com.jeesharp.business.modules.food.vo.FoodPackageQueryVO;
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.*;

/**
 * 管理端美食团购套餐接口：可售套餐、库存与价格维护。
 *
 * <p>C 端团购下单见 {@link com.jeesharp.business.food.controller.FoodMobileController}。</p>
 *
 * @author jeesharp
 * @since 2026-06-04
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/food/package/v1")
@Tag(name = "美食套餐管理")
@AllArgsConstructor
public class FoodPackageController extends BaseController {

    private static final String SAVE_SUCCESS_MESSAGE = "保存成功";
    private static final String DELETE_SUCCESS_MESSAGE = "删除成功";

    private final FoodPackageApplicationService foodPackageApplicationService;

    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('manager:business:food:package:info')")
    @GetMapping("info")
    public AjaxResult<FoodPackageInfoVO> info(@RequestParam("id") String id) {
        FoodPackageInfoVO vo = foodPackageApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "获取列表（分页）")
    @PreAuthorize("@ss.hasPermission('manager:business:food:package:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<FoodPackageListVO>> list(@RequestBody FoodPackageQueryVO query) {
        PageRespVO<FoodPackageListVO> page = foodPackageApplicationService.findPage(new Page<FoodPackage>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('manager:business:food:package:save') or @ss.hasPermission('manager:business:food:package:create') or @ss.hasPermission('manager:business:food:package:update')")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody @Valid FoodPackageSaveDTO dto) {
        foodPackageApplicationService.save(dto);
        return AjaxResult.ok(SAVE_SUCCESS_MESSAGE);
    }

    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('manager:business:food:package:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        foodPackageApplicationService.delete(ids);
        return AjaxResult.ok(DELETE_SUCCESS_MESSAGE);
    }
}
