package com.starry.admin.modules.order.service;

import static org.junit.jupiter.api.Assertions.*;

import com.starry.admin.modules.order.module.constant.OrderConstant;
import com.starry.admin.modules.order.module.dto.CommodityInfo;
import com.starry.admin.modules.order.module.dto.OrderCreationContext;
import com.starry.admin.modules.order.module.dto.PaymentInfo;
import com.starry.admin.modules.order.module.dto.RandomOrderRequirements;
import java.math.BigDecimal;
import java.util.Arrays;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
 * 订单创建请求对象测试类
 *
 * @author admin
 */
class OrderCreationContextTest {

    @Test
    @DisplayName("测试Builder模式构建订单请求")
    void testBuilderPattern() {
        // 构建商品信息
        CommodityInfo commodityInfo = CommodityInfo.builder()
                .commodityId("commodity_001")
                .commodityType(OrderConstant.CommodityType.SERVICE)
                .commodityPrice(new BigDecimal("100.00"))
                .serviceDuration("60")
                .commodityName("陪聊服务")
                .commodityNumber("1")
                .build();

        // 构建支付信息
        PaymentInfo paymentInfo = PaymentInfo.builder()
                .orderMoney(new BigDecimal("100.00"))
                .finalAmount(new BigDecimal("90.00"))
                .discountAmount(new BigDecimal("10.00"))
                .couponIds(Arrays.asList("coupon_001"))
                .payMethod("0")
                .build();

        // 构建订单请求
        OrderCreationContext request = OrderCreationContext.builder()
                .orderId("order_123456")
                .orderNo("ORD20240906001")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.SPECIFIED)
                .rewardType(OrderConstant.RewardType.BALANCE)
                .isFirstOrder(true)
                .commodityInfo(commodityInfo)
                .paymentInfo(paymentInfo)
                .purchaserBy("customer_001")
                .acceptBy("clerk_001")
                .weiChatCode("wx123456")
                .remark("客户备注信息")
                .build();

        // 验证构建结果
        assertEquals("order_123456", request.getOrderId());
        assertEquals("ORD20240906001", request.getOrderNo());
        assertEquals(OrderConstant.OrderStatus.PENDING, request.getOrderStatus());
        assertEquals(OrderConstant.OrderType.NORMAL, request.getOrderType());
        assertEquals(OrderConstant.PlaceType.SPECIFIED, request.getPlaceType());
        assertTrue(request.isFirstOrder());
        assertEquals("1", request.getFirstOrderString());

        // 验证商品信息
        assertNotNull(request.getCommodityInfo());
        assertEquals("commodity_001", request.getCommodityInfo().getCommodityId());
        assertEquals(new BigDecimal("100.00"), request.getCommodityInfo().getCommodityPrice());

        // 验证支付信息
        assertNotNull(request.getPaymentInfo());
        assertEquals(new BigDecimal("90.00"), request.getPaymentInfo().getFinalAmount());
        assertEquals(1, request.getPaymentInfo().getCouponIds().size());
    }

    @Test
    @DisplayName("测试订单类型判断方法")
    void testOrderTypeChecks() {
        // 测试指定单
        OrderCreationContext specifiedOrder = OrderCreationContext.builder()
                .orderId("order_001")
                .orderNo("ORD001")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.SPECIFIED)
                .isFirstOrder(false)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .build();

        assertTrue(specifiedOrder.isSpecifiedOrder());
        assertFalse(specifiedOrder.isValidForRandomOrder());
        assertFalse(specifiedOrder.isRewardOrder());

        // 测试随机单
        OrderCreationContext randomOrder = OrderCreationContext.builder()
                .orderId("order_002")
                .orderNo("ORD002")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.RANDOM)
                .isFirstOrder(false)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .randomOrderRequirements(RandomOrderRequirements.builder()
                        .clerkGender(OrderConstant.Gender.FEMALE)
                        .clerkLevelId("level_001")
                        .excludeHistory("1")
                        .build())
                .build();

        assertFalse(randomOrder.isSpecifiedOrder());
        assertTrue(randomOrder.isValidForRandomOrder());
        assertFalse(randomOrder.isRewardOrder());

        // 测试打赏单
        OrderCreationContext rewardOrder = OrderCreationContext.builder()
                .orderId("order_003")
                .orderNo("ORD003")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.REWARD)
                .isFirstOrder(false)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .build();

        assertFalse(rewardOrder.isSpecifiedOrder());
        assertFalse(rewardOrder.isValidForRandomOrder());
        assertTrue(rewardOrder.isRewardOrder());
    }

    @Test
    @DisplayName("测试首单标识转换")
    void testFirstOrderStringConversion() {
        // 测试首单
        OrderCreationContext firstOrder = OrderCreationContext.builder()
                .orderId("order_001")
                .orderNo("ORD001")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.SPECIFIED)
                .isFirstOrder(true)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .build();

        assertEquals("1", firstOrder.getFirstOrderString());

        // 测试非首单
        OrderCreationContext notFirstOrder = OrderCreationContext.builder()
                .orderId("order_002")
                .orderNo("ORD002")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.SPECIFIED)
                .isFirstOrder(false)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .build();

        assertEquals("0", notFirstOrder.getFirstOrderString());
    }

    @Test
    @DisplayName("测试随机单验证逻辑")
    void testRandomOrderValidation() {
        // 有效的随机单
        OrderCreationContext validRandomOrder = OrderCreationContext.builder()
                .orderId("order_001")
                .orderNo("ORD001")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.RANDOM)
                .isFirstOrder(false)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .randomOrderRequirements(RandomOrderRequirements.builder()
                        .clerkGender(OrderConstant.Gender.FEMALE)
                        .build())
                .build();

        assertTrue(validRandomOrder.isValidForRandomOrder());

        // 无效的随机单（缺少要求信息）
        OrderCreationContext invalidRandomOrder = OrderCreationContext.builder()
                .orderId("order_002")
                .orderNo("ORD002")
                .orderStatus(OrderConstant.OrderStatus.PENDING)
                .orderType(OrderConstant.OrderType.NORMAL)
                .placeType(OrderConstant.PlaceType.RANDOM)
                .isFirstOrder(false)
                .commodityInfo(CommodityInfo.builder().commodityId("test").build())
                .paymentInfo(PaymentInfo.builder().orderMoney(BigDecimal.ZERO).build())
                .purchaserBy("customer")
                .build();

        assertFalse(invalidRandomOrder.isValidForRandomOrder());
    }
}
