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

import com.jeesharp.business.modules.food.application.FoodCompanyRechargeApplicationService;
import com.jeesharp.business.modules.food.dto.FoodCompanyRechargeConfirmDTO;
import com.jeesharp.business.modules.food.dto.FoodCompanyRechargeCreateDTO;
import com.jeesharp.business.modules.food.dto.FoodCompanyRechargePaidDTO;
import com.jeesharp.business.modules.food.entity.FoodCompanyAccountLog;
import com.jeesharp.business.modules.food.vo.FoodCompanyAccountInfoVO;
import com.jeesharp.business.modules.food.vo.FoodCompanyAccountLogListVO;
import com.jeesharp.business.modules.food.vo.FoodCompanyAccountLogQueryVO;
import com.jeesharp.business.modules.food.vo.FoodCompanyRechargeResultVO;
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.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企业团餐充值管理接口。
 *
 * @author jeesharp
 * @since 2026-06-07
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/food/company/recharge/v1")
@Tag(name = "企业团餐充值管理")
@AllArgsConstructor
public class FoodCompanyRechargeController extends BaseController {

    private final FoodCompanyRechargeApplicationService foodCompanyRechargeApplicationService;

    @Operation(summary = "创建线下充值单")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:recharge:create')")
    @PostMapping("offline/create")
    public AjaxResult<FoodCompanyRechargeResultVO> createOffline(@RequestBody @Valid FoodCompanyRechargeCreateDTO dto) {
        return AjaxResult.okData(foodCompanyRechargeApplicationService.createOfflineRecharge(dto));
    }

    @Operation(summary = "确认线下充值入账")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:recharge:confirm')")
    @PostMapping("offline/confirm")
    public AjaxResult<FoodCompanyRechargeResultVO> confirmOffline(@RequestBody @Valid FoodCompanyRechargeConfirmDTO dto) {
        return AjaxResult.okData(foodCompanyRechargeApplicationService.confirmOfflineRecharge(dto));
    }

    @Operation(summary = "创建线上充值单")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:recharge:create')")
    @PostMapping("online/create")
    public AjaxResult<FoodCompanyRechargeResultVO> createOnline(@RequestBody @Valid FoodCompanyRechargeCreateDTO dto) {
        return AjaxResult.okData(foodCompanyRechargeApplicationService.createOnlineRecharge(dto));
    }

    @Operation(summary = "线上充值支付成功入账")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:recharge:paid')")
    @PostMapping("online/paid")
    public AjaxResult<FoodCompanyRechargeResultVO> onlinePaid(@RequestBody @Valid FoodCompanyRechargePaidDTO dto) {
        return AjaxResult.okData(foodCompanyRechargeApplicationService.handleOnlineRechargePaid(dto));
    }

    @Operation(summary = "查询企业账户余额")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:account:info')")
    @GetMapping("account/info")
    public AjaxResult<FoodCompanyAccountInfoVO> accountInfo(@RequestParam("companyId") String companyId) {
        return AjaxResult.okData(foodCompanyRechargeApplicationService.getAccountByCompanyId(companyId));
    }

    @Operation(summary = "查询企业账户流水")
    @PreAuthorize("@ss.hasPermission('manager:business:food:company:account:log:list')")
    @PostMapping("account/log/list")
    public AjaxResult<PageRespVO<FoodCompanyAccountLogListVO>> accountLogList(@RequestBody FoodCompanyAccountLogQueryVO query) {
        PageRespVO<FoodCompanyAccountLogListVO> page = foodCompanyRechargeApplicationService.findAccountLogPage(new Page<FoodCompanyAccountLog>(query), query);
        return AjaxResult.okData(page);
    }
}
