package edu.whut.trigger.http;

import com.alibaba.fastjson.JSON;
import edu.whut.api.IMarketTradeService;
import edu.whut.api.dto.*;
import edu.whut.api.response.Response;
import edu.whut.domain.activity.model.entity.MarketProductEntity;
import edu.whut.domain.activity.model.entity.TrialBalanceEntity;
import edu.whut.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import edu.whut.domain.activity.service.IIndexGroupBuyMarketService;
import edu.whut.domain.trade.model.entity.*;
import edu.whut.domain.trade.model.valobj.GroupBuyProgressVO;
import edu.whut.domain.trade.model.valobj.NotifyConfigVO;
import edu.whut.domain.trade.model.valobj.NotifyTypeEnumVO;
import edu.whut.domain.trade.model.valobj.TradeOrderStatusEnumVO;
import edu.whut.domain.trade.service.ITradeLockOrderService;
import edu.whut.domain.trade.service.ITradeRefundOrderService;
import edu.whut.domain.trade.service.ITradeSettlementOrderService;
import edu.whut.trigger.basic.MarketTradeService;
import edu.whut.types.enums.ResponseCode;
import edu.whut.types.exception.AppException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 营销交易服务
 * 对接 HTTP 层，只做「适配 + 编排」，
 * 具体业务规则（价格计算、拼团人数校验等）下沉到 Domain / Application Service。
 * 锁单完整流程：
 *     基础参数校验
 *     判断是否已有未支付订单
 *     （拼团场景）校验团队目标是否已达成
 *     向营销服务做一次优惠试算
 *     组装领域对象并调用交易应用服务锁单
 *     返回锁单结果
 */
@Slf4j
@RestController
@CrossOrigin("*")
@RequiredArgsConstructor
@RequestMapping("/api/v1/gbm/trade")
public class MarketTradeController {

    private final MarketTradeService marketTradeService;

    /**
     * 锁定营销预支付订单（拼团 / 普通优惠）
     */
    @PostMapping("/lock_market_pay_order")
    public Response<LockMarketPayOrderResponseDTO> lockMarketPayOrder(@RequestBody LockMarketPayOrderRequestDTO lockMarketPayOrderRequestDTO) {
        return marketTradeService.lockMarketPayOrder(lockMarketPayOrderRequestDTO);
    }

    /**
     * 拼团交易结算
     * @param requestDTO 结算商品信息
     * @return
     */
    @PostMapping("/settlement_market_pay_order")
    public Response<SettlementMarketPayOrderResponseDTO> settlementMarketPayOrder(@RequestBody SettlementMarketPayOrderRequestDTO requestDTO) {
        return marketTradeService.settlementMarketPayOrder(requestDTO);
    }


    /**
     * 用户退单接口
     * @param requestDTO 退单请求信息
     * @return
     */
    @PostMapping("/refund_market_pay_order")
    public Response<RefundMarketPayOrderResponseDTO> refundMarketPayOrder(@RequestBody RefundMarketPayOrderRequestDTO requestDTO) {
        return marketTradeService.refundMarketPayOrder(requestDTO);
    }
}
