package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.ext.PageResult;
import com.fuint.common.dto.ext.ShopAlertRequest;
import com.fuint.common.service.ShopAlertService;
import com.fuint.common.vo.ShopDetailVO;
import com.fuint.common.vo.ShopVO;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.repository.model.MtStore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Survive
 * @date 2025/3/12
 * @description TODO
 */
@Api(tags="管理端-预警相关接口")
@RestController
@AllArgsConstructor
@RequestMapping("/backendApi/shop-alerts")
public class ShopAlertController extends BaseController {
    private ShopAlertService shopService;

    /**
     * 分页数据展示
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping(value = "/list")
    @CrossOrigin
    @ApiOperation(value = "店铺列表")
    public PageResult<ShopVO> listShops(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name
    ) throws BusinessCheckException{
        PageResult<ShopVO> result = shopService.listShops(page, size, name);
        return result;
    }


    /**
     * 店铺详情
     * @param shopId
     * @param withOrders 否携带当日订单
     * @return
     */
    @ApiOperation(value = "店铺详情")
    @GetMapping("/{shopId}")
    @CrossOrigin
//    @PreAuthorize("@pms.hasPermission('alerts:index')")
    public ResponseObject getShopDetail(HttpServletRequest request, @PathVariable Long shopId, @RequestParam(required = false) Boolean withOrders  ) throws BusinessCheckException {
        ShopDetailVO shopDetail = shopService.getShopDetail(shopId, withOrders);
        return getSuccessResult(shopDetail);
    }

    /**
     * 新增预警信息
     * @param request
     * @return
     */
    @ApiOperation(value = "新增预警")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @CrossOrigin
//    @PreAuthorize("@pms.hasPermission('alerts:index')")
    public ResponseObject createAlert(HttpServletRequest request, @RequestBody ShopAlertRequest alertInfo) throws BusinessCheckException {
        shopService.addShopAlert(alertInfo.getId(),
                alertInfo.getMail(),
                alertInfo.getThreshold());
        return getSuccessResult(true);
    }

    /**
     * 更新预警信息
     * @param shopId
     * @param request
     * @return
     */
    @ApiOperation(value = "更新预警")
    @CrossOrigin
    @PostMapping("/update/{shopId}")
    //    @PreAuthorize("@pms.hasPermission('alerts:index')")
    public ResponseObject updateShopAlert(@PathVariable Long shopId, @RequestBody ShopAlertRequest request) throws BusinessCheckException{
        MtStore updatedShop = shopService.updateShopAlert(shopId, request.getMail(), request.getThreshold());
        return getSuccessResult("更新成功",updatedShop);
    }

    /**
     * 清除预警信息
     * @param shopId
     * @return
     */
    @ApiOperation(value = "清除预警配置信息")
    @CrossOrigin
    @PostMapping("/clear/{shopId}")
    //    @PreAuthorize("@pms.hasPermission('alerts:index')")
    public ResponseObject clearAlertConfig ( @PathVariable Long shopId ) throws BusinessCheckException{
        MtStore updatedShop = shopService.clearAlertConfig(shopId);
        return getSuccessResult("预警配置已清除", updatedShop);
    }
}