package com.yc.ship.module.trade.controller.app.otc;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.ip.core.utils.AreaUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.framework.security.core.LoginUser;
import com.yc.ship.framework.security.core.annotations.PreAuthenticated;
import com.yc.ship.module.product.controller.admin.voyage.vo.VoyageRespVO;
import com.yc.ship.module.product.dal.dataobject.voyage.VoyageDO;
import com.yc.ship.module.product.service.voyage.VoyageService;
import com.yc.ship.module.resource.api.route.RouteApi;
import com.yc.ship.module.resource.api.route.dto.RouteRespDTO;
import com.yc.ship.module.resource.api.ship.ShipApi;
import com.yc.ship.module.resource.api.ship.dto.ShipRespDTO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.*;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.TradeRefundCalculateRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.TradeRefundCreateReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.TradeRefundCreateRespVO;
import com.yc.ship.module.trade.controller.admin.orderjzdetail.vo.OrderJzDetailSaveReqVO;
import com.yc.ship.module.trade.controller.app.otc.vo.*;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderCountryDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderTotalDO;
import com.yc.ship.module.trade.enums.RefundChannelEnum;
import com.yc.ship.module.trade.framework.annotation.RequestDecryption;
import com.yc.ship.module.trade.service.order.AdminTradeOrderService;
import com.yc.ship.module.trade.service.order.TradeOrderCountryService;
import com.yc.ship.module.trade.service.order.TradeOrderService;
import com.yc.ship.module.trade.service.order.TradeOrderTotalService;
import com.yc.ship.module.trade.service.orderjzdetail.OrderJzDetailService;
import com.yc.ship.module.trade.service.otc.AppOtcTradeOrderService;
import com.yc.ship.module.trade.service.otc.OtcTradeOrderService;
import com.yc.ship.module.trade.utils.AgencyAuthUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.TRADE;
import static com.yc.ship.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.yc.ship.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static com.yc.ship.framework.web.core.util.WebFrameworkUtils.getTenantId;


/**
 * &#064;Description ：旅行社APP交易服务controller, 自助机、小程序
 *
 * @author ：qsl
 * &#064;Date ：2025/6/17 20:52
 */
@Tag(name = "旅行社APP(自助机、小程序)交易服务")
@RestController
@RequestMapping("/app/mini/trade")
@Validated
@Slf4j
public class AppOtcTradeOrderController {

    @Resource
    private AppOtcTradeOrderService appOtcTradeOrderService;
    @Resource
    private AdminTradeOrderService adminTradeOrderService;
    @Resource
    private AgencyAuthUtils agencyAuthUtils;

    @Resource
    private TradeOrderService tradeOrderService;

    @Resource
    private OtcTradeOrderService otcTradeOrderService;



    @RequestDecryption
    @PostMapping("/miniApp/createOrder")
    @Operation(summary = "小程序创建订单")
    @PreAuthenticated
    @OperateLog(type = TRADE)
    public CommonResult<OtcTradeOrderCreateRespVO> miniAppCreateOrder(@Valid @RequestBody MiniTradeOrderCreateReqVO otcTradeOrderCreateReqVO) {
        LoginUser loginUser = getLoginUser();
        return appOtcTradeOrderService.miniAppCreateOrder(loginUser, otcTradeOrderCreateReqVO);
    }

    @RequestDecryption
    @PostMapping("/miniApp/payOrder")
    @Operation(summary = "小程序支付订单")
    @PreAuthenticated
    @OperateLog(type = TRADE)
    public CommonResult<PayOrderRespVO> miniAppPayOrder(@Valid @RequestBody PayOrderReqVO payOrderReqVO) {
        return appOtcTradeOrderService.miniAppPayOrder(getLoginUser(), payOrderReqVO);
    }

    @PostMapping("/miniApp/refund")
    @Operation(summary = "小程序退款")
    @PreAuthenticated
    @OperateLog(type = TRADE)
    public CommonResult<TradeRefundCreateRespVO> miniAppRefund(@Valid @RequestBody TradeRefundCreateReqVO refundCreateReqVO) {
        refundCreateReqVO.setRefundChannel(RefundChannelEnum.MINI_APP.getType());
        return adminTradeOrderService.minirefund(getLoginUser(), refundCreateReqVO);
    }

    @PostMapping("/miniApp/orderList")
    @Operation(summary = "小程序-订单列表[分页]", description = "根据条件查询订单列表")
    @PreAuthenticated
    @OperateLog(type = API)
    public CommonResult<PageResult<AppTradeOrderRespNewVO>> orderList(@Valid @RequestBody AppTradeOrderPageReqNewVO pageReqVO) {
        Long loginUserId = getLoginUserId();
        pageReqVO.setMemberId(String.valueOf(loginUserId));
        PageResult<AppTradeOrderRespNewVO> page = tradeOrderService.getAppOrderPage(pageReqVO);
        return success(page);
    }

    @GetMapping("/miniApp/getOrderInfo")
    @Operation(summary = "小程序-查询订单详情", description = "根据订单id查询查询订单详情数据")
    @PreAuthenticated
    @OperateLog(type = API)
    public CommonResult<AppTradeOrderRespVO> getOrderInfo(@Valid Long id) {
        return appOtcTradeOrderService.miniOrderInfoById(id);
    }

    @GetMapping("/miniApp/orderDetailInfo")
    @Operation(summary = "小程序-门票详情", description = "根据门票id查询门票详情")
    @OperateLog(type = API)
    public CommonResult<AppTradeOrderDetailRespVO> getOrderInfoNew(@Valid @RequestParam("id") Long id) {
        return success(otcTradeOrderService.getAppOrderInfoNew(id));
    }


    @GetMapping("/miniApp/getConfig")
    @Operation(summary = "小程序-查询参数值")
    @OperateLog(type = API)
    public CommonResult<String> getConfig(@Valid @RequestParam("key") String key) {
        return CommonResult.success(appOtcTradeOrderService.getConfigValue(key));
    }

    @RequestDecryption
    @PutMapping("/miniApp/cancelOrder")
    @Operation(summary = "小程序取消订单")
    @PreAuthenticated
    @OperateLog(type = TRADE)
    public CommonResult<?> miniAppCancelOrder(@RequestParam("orderId") Long orderId) {
        return appOtcTradeOrderService.miniAppCancelOrder(getLoginUser(), orderId);
    }

    @PostMapping("/miniApp/calculateRefundAmount")
    @Operation(summary = "小程序退款计算")
    @OperateLog(type = TRADE)
    public CommonResult<TradeRefundCalculateRespVO> calculateRefundAmount(@Valid @RequestBody TradeRefundCreateReqVO refundCreateReqVO) {
        refundCreateReqVO.setRefundChannel(RefundChannelEnum.MINI_APP.getType());
        return adminTradeOrderService.calculateRefundAmount(agencyAuthUtils.getAgencyLoginUser(), refundCreateReqVO);
    }

    // 查询我的行程
    @GetMapping("/miniApp/getMyItinerary")
    @Operation(summary = "查询我的行程")
    @PreAuthenticated
    @OperateLog(type = API)
    public CommonResult<List<AppMyItineraryVO>> getMyItinerary(@RequestParam("status") Integer status, @RequestParam(value = "keyword", required = false) String keyword) {
        Long loginUserId = getLoginUserId();
        List<AppMyItineraryVO> list = appOtcTradeOrderService.getMyItinerary(loginUserId, status, keyword);
        return CommonResult.success(list);
    }


    // 查询行程详情
    @GetMapping("/miniApp/getItineraryDetail")
    @Operation(summary = "查询行程详情")
    @PreAuthenticated
    @OperateLog(type = API)
    public CommonResult<AppItineraryDetailVO> getItineraryDetail(@RequestParam("id") Long orderId, @RequestParam(value = "keyword", required = false) String keyword) {
        AppItineraryDetailVO detailVO = appOtcTradeOrderService.getItineraryDetail(orderId, keyword);
        return CommonResult.success(detailVO);
    }

    @PostMapping("/miniApp/sign")
    @Operation(summary = "接站登记")
    @PreAuthenticated
    @OperateLog(type = API)
    public CommonResult<Long> createOrderJzDetail(@Valid @RequestBody AppOrderJzReqVO reqVO) {
        return success(appOtcTradeOrderService.appCreateOrderJzDetail(reqVO));
    }


}
