package com.jeesharp.business.member.controller;

import com.jeesharp.business.modules.member.application.MemberIapOrderApplicationService;
import com.jeesharp.business.modules.member.dto.VipIapConfirmPurchaseDTO;
import com.jeesharp.business.modules.member.dto.VipIapCreateOrderDTO;
import com.jeesharp.business.modules.member.vo.AppIapOrderQueryVO;
import com.jeesharp.business.modules.member.vo.VipIapOrderVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
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.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;

/**
 * 移动端 VIP IAP 订单（创建、确认、查询）
 *
 * @author jeesharp
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/vipIap")
@Tag(name = "移动端-VIP IAP订单", description = "华为 IAP 业务订单")
@AllArgsConstructor
public class VipIapOrderMobileController extends BaseMobileController {

    private final MemberIapOrderApplicationService memberIapOrderApplicationService;

    @Operation(summary = "创建订单（拉起收银台前）")
    @PostMapping("/createOrder")
    public AjaxResult<VipIapOrderVO> createOrder(@RequestBody @Valid VipIapCreateOrderDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberIapOrderApplicationService.createOrder(memberId, dto));
    }

    @Operation(summary = "支付成功后确认（幂等发货）")
    @PostMapping("/confirmPurchase")
    public AjaxResult<VipIapOrderVO> confirmPurchase(@RequestBody @Valid VipIapConfirmPurchaseDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberIapOrderApplicationService.confirmPurchase(memberId, dto));
    }

    @Operation(summary = "查询订单状态")
    @GetMapping("/info")
    public AjaxResult<VipIapOrderVO> info(@RequestParam("orderNo") String orderNo) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberIapOrderApplicationService.getOrderForMember(memberId, orderNo));
    }

    @Operation(summary = "我的 IAP 订单列表（分页）")
    @PostMapping("/list")
    public AjaxResult<Page<VipIapOrderVO>> list(@RequestBody(required = false) AppIapOrderQueryVO query) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberIapOrderApplicationService.findOrderPage(memberId, query));
    }
}
