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

import com.jeesharp.business.modules.food.application.FoodCompanyMealApplicationService;
import com.jeesharp.business.modules.food.application.FoodPersonalMealApplicationService;
import com.jeesharp.business.modules.food.dto.FoodMealOrderRefundApplyDTO;
import com.jeesharp.business.modules.food.dto.FoodMealOrderVerifyDTO;
import com.jeesharp.business.modules.food.dto.FoodPersonalMealRefundDTO;
import com.jeesharp.business.modules.food.dto.FoodPersonalMealRefundTriggerDTO;
import com.jeesharp.business.modules.food.entity.FoodMealOrder;
import com.jeesharp.business.modules.food.vo.FoodMealOrderQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.utils.WebFrameworkUtils;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理端企业团餐订餐单接口：列表查询、退款申请与取餐核销。
 *
 * <p>个人订餐 C 端见 {@link com.jeesharp.business.food.controller.FoodMealMobileController} 与
 * {@link com.jeesharp.business.modules.food.application.FoodPersonalMealApplicationService}。</p>
 *
 * @author jeesharp
 * @since 2026-06-07
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/food/mealOrder/v1")
@Tag(name = "订餐订单管理")
@AllArgsConstructor
public class FoodMealOrderController extends BaseController {

    private final FoodCompanyMealApplicationService foodCompanyMealApplicationService;
    private final FoodPersonalMealApplicationService foodPersonalMealApplicationService;

    @Operation(summary = "获取列表（分页）")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<FoodMealOrder>> list(@RequestBody(required = false) FoodMealOrderQueryVO query) {
        FoodMealOrderQueryVO q = query != null ? query : new FoodMealOrderQueryVO();
        Page<FoodMealOrder> result = foodCompanyMealApplicationService.findOrderPage(new Page<>(q), q);
        PageRespVO<FoodMealOrder> resp = new PageRespVO<>();
        resp.setRecords(result.getRecords());
        resp.setTotal(result.getTotal());
        resp.setPageNo(result.getPageNo());
        resp.setPageSize(result.getPageSize());
        return AjaxResult.okData(resp);
    }

    @Operation(summary = "企业团餐退款申请")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:refund')")
    @PostMapping("refund/apply")
    public AjaxResult<Void> applyRefund(@RequestBody @Valid FoodMealOrderRefundApplyDTO dto) {
        foodCompanyMealApplicationService.applyRefund(dto, null);
        return AjaxResult.ok("退款申请已发起");
    }

    @Operation(summary = "企业团餐核销/取餐")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:verify')")
    @PostMapping("verify")
    public AjaxResult<Void> verify(@RequestBody @Valid FoodMealOrderVerifyDTO dto) {
        dto.setVerifyUserId(WebFrameworkUtils.getLoginUserId());
        dto.setVerifyUserName(WebFrameworkUtils.getLoginUserName());
        foodCompanyMealApplicationService.verify(dto);
        return AjaxResult.ok("核销成功");
    }

    @Operation(summary = "个人订餐发起微信退款")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:refund')")
    @PostMapping("personal/refund/trigger")
    public AjaxResult<String> triggerPersonalRefund(@RequestBody @Valid FoodPersonalMealRefundTriggerDTO dto) {
        dto.setOperatorId(WebFrameworkUtils.getLoginUserId());
        return AjaxResult.okData(foodPersonalMealApplicationService.triggerMealRefund(dto));
    }

    @Operation(summary = "个人订餐退款完成补偿")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:refund')")
    @PostMapping("personal/refund")
    public AjaxResult<Void> confirmPersonalRefund(@RequestBody @Valid FoodPersonalMealRefundDTO dto) {
        foodPersonalMealApplicationService.confirmMealRefund(dto);
        return AjaxResult.ok("退款状态已同步");
    }

    @Operation(summary = "个人订餐取餐核销")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:verify')")
    @PostMapping("personal/verify")
    public AjaxResult<Void> verifyPersonal(@RequestBody @Valid FoodMealOrderVerifyDTO dto) {
        dto.setVerifyUserId(WebFrameworkUtils.getLoginUserId());
        dto.setVerifyUserName(WebFrameworkUtils.getLoginUserName());
        foodPersonalMealApplicationService.verify(dto);
        return AjaxResult.ok("核销成功");
    }

    @Operation(summary = "企业团餐发起微信退款")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:refund')")
    @PostMapping("company/refund/trigger")
    public AjaxResult<String> triggerCompanyRefund(@RequestBody @Valid FoodPersonalMealRefundTriggerDTO dto) {
        dto.setOperatorId(WebFrameworkUtils.getLoginUserId());
        return AjaxResult.okData(foodCompanyMealApplicationService.triggerCompanyMealRefund(dto));
    }

    @Operation(summary = "企业团餐退款完成补偿")
    @PreAuthorize("@ss.hasPermission('manager:business:food:mealOrder:refund')")
    @PostMapping("company/refund")
    public AjaxResult<Void> confirmCompanyRefund(@RequestBody @Valid FoodPersonalMealRefundDTO dto) {
        foodCompanyMealApplicationService.confirmCompanyMealRefund(dto);
        return AjaxResult.ok("退款状态已同步");
    }
}
