package com.yc.ship.module.trade.api.dto;

import com.yc.ship.framework.common.validation.IntEnum;
import com.yc.ship.framework.common.validation.Mobile;
import com.yc.ship.module.trade.enums.CredentialTypeEnum;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Schema(description = "RPC 服务 - 订单创建 Request DTO")
@Data
public class TradeOrderCreateReqDTO implements Serializable {

    @Schema(description = "订单ID，改签下单传入", example = "MT1231312")
    @Hidden
    private Long orderId;

    @Schema(description = "第三方订单号，分销商下单", example = "MT1231312")
    @Hidden
    private String externalOrderNo;

    @Schema(description = "第三方原始订单号，内部旅行社下单", example = "MT1231312")
    @Hidden
    private String externalOriginOrderNo;

    @Schema(description = "出行日期", example = "2025-06-01")
    private Date travelDate;

    @Schema(description = "订单项列表", example = "")
    @NotEmpty(message = "订单项列表不能为空")
    @Valid
    private List<OrderItem> itemlList;

    @Schema(description = "订单金额（元）", example = "100")
    @NotNull(message = "订单金额不能为空")
    private BigDecimal amount;

    @Schema(description = "支付金额（元）", example = "90")
    @NotNull(message = "支付金额不能为空")
    private BigDecimal payAmount;

    @Schema(description = "订单联系信息", example = "")
    @NotNull(message = "联系人信息不能为空")
    private OrderUser orderUser;

    @Schema(description = "订单属性", example = "")
    private OrderProperty orderProperty;

    @Schema(description = "订单备注", example = "xx游客岁数较大")
    @Length(max = 120,message = "备注不能超过120个字符")
    private String remark;

    @Schema(description = "游玩时间及时间方案，单产品时传入", example = "")
    private Long routePlanId;

    @Schema(description = "区域经理", example = "")
    private Long areaManagerId;

    @Schema(description = "携程需要参数，其他情况忽略", example = "")
    private String itemId;

    @Schema(description = "是否完全支付 0 否 1是", example = "")
    private Integer isFullPay;

    @Schema(description = "是否改签 0 否 1是", example = "")
    private Integer isChange;

    @Schema(description = "是否补票 0 否 1是", example = "")
    private Integer isTimePay;

    @Schema(description = "主订单ID，通过此订单再新增订单（增加产品/数量，改签时传递）", example = "")
    private Long mainOrderId;

    @Schema(description = "是否申请营销政策（0否 1是）", example = "")
    private Integer isMarketing;

    @Schema(description = "是否申请使用营销政策（0否 1是）", example = "")
    private Integer isMarketingUse;

    @Schema(description = "组团社ID", example = "")
    private Long agencyGroupId;

    @Schema(description = "是否为增加订单 0 否 1是", example = "")
    private Integer isAddOrder;

    @Schema(description = "团号", example = "")
    private String groupNo;

    @Schema(description = "交付信息", example = "")
    private DeliveryInfo deliveryInfo;

    @Schema(description = "打包产品属性,目前指小程序子产品", example = "")
    private List<PackProduct> packProductList;

    /**
     * 航次ID
     */
    @Schema(description = "航次ID")
    private Long voyageId;

    /**
     * 游轮ID
     */
    @Schema(description = "游轮ID")
    private Long shipId;

    /**
     * 联系人
     */
    @Schema(description = "联系人")
    private String linkMan;

    /**
     * 联系电话
     */
    @Schema(description = "联系电话")
    private String linkMobile;
    /**
     * 联系人邮箱
     */
    @Schema(description = "联系人邮箱")
    private String linkEmail;

    /**
     * 币种
     */
    @Schema(description = "币种")
    private String currency;

    @Schema(description = "订单项")
    @Data
    @Valid
    public static class OrderItem{

        @Schema(description = "游玩时间及时间方案,多产品下单时传入", example = "")
        private Long routePlanId;

        @Schema(description = "打包产品ID", example = "")
        private Long packProductId;

        @Schema(description = "产品ID", example = "")
        @NotBlank(message = "产品ID不能为空")
        private String productId;

        @Schema(description = "产品类型 0 自建产品 1平台产品", example = "")
        @NotNull(message = "产品类型不能为空")
        private Integer productType;

        @Schema(description = "出行日期", example = "2025-06-01")
        private Date useDate;

        @Schema(description = "酒店预定信息，小程序下单", example = "")
        private HotelBooking hotelBooking;

        @Schema(description = "订单明细列表", example = "")
        @NotEmpty(message = "订单明细列表不能为空")
        @Valid
        private List<OrderDetail> detailList;

    }

    @Schema(description = "订单明细")
    @Data
    @Valid
    public static class OrderDetail{

        @Schema(description = "产品ID", example = "")
        @NotBlank(message = "产品ID不能为空")
        private String productId;

        @Schema(description = "产品类型 0 自建产品 1平台产品", example = "")
        @NotNull(message = "产品类型不能为空")
        private Integer productType;

        @Schema(description = "单价", example = "")
        @NotNull(message = "单价不能为空")
        private BigDecimal price;

        @Schema(description = "原始单价", example = "")
        @NotNull(message = "原始单价不能为空")
        private BigDecimal originPrice;

        @Schema(description = "规格类型", example = "full")
//        @InStringEnum(value = SpecTypeEnum.class,message = "规格类型不正确")
        @NotBlank(message = "规格类型不能为空")
        private String specType;

        @Schema(description = "购买数量", example = "10")
        @NotNull(message = "购买数量不能为空")
        private Integer count;

        @Schema(description = "明细基础", example = "")
        @NotEmpty(message = "明细基础不能为空")
        @Valid
        private List<OrderDetailBase> detailBaseList;

        @Schema(description = "游客列表", example = "")
        @Valid
        private List<Visitor> visitorList;

        @Schema(description = "游客列表（一票多人）", example = "")
        @Valid
        private List<MultilVisitor> multilVisitorList;

        @Schema(description = "价格列表", example = "")
        @Valid
        private List<DetailPrice> detailPriceList;
    }

    @Schema(description = "订单明细基础")
    @Data
    @Valid
    public static class OrderDetailBase{


        @Schema(description = "基础产品ID", example = "")
        @NotNull(message = "基础产品ID不能为空")
        private Long productBaseId;

        @Schema(description = "项目ID", example = "")
        @NotNull(message = "项目ID不能为空")
        private Long projectId;

        @Schema(description = "规格ID", example = "")
        @NotNull(message = "规格ID不能为空")
        private Long specificationId;

        @Schema(description = "规格类型", example = "full")
//        @InStringEnum(value = SpecTypeEnum.class,message = "规格类型不正确")
        @NotBlank(message = "规格类型不能为空")
        private String specType;

        @Schema(description = "平台分销的产品编码，平台分销产品必传", example = "123")
        private String platSupplierProductId;

        @Schema(description = "使用日期，定制游时可设置", example = "2025-06-01")
        private Date useDate;

        @Schema(description = "产品资源ID，定制游时可设置", example = "2025-06-01")
        private Long productResourceId;

        @Schema(description = "成本价，定制游时可设置", example = "2025-06-01")
        private BigDecimal costPrice;
    }

    @Schema(description = "游客信息")
    @Data
    @Valid
    public static class Visitor{

        @Schema(description = "姓名", example = "")
        private String name;

        @Schema(description = "证件号", example = "")
        private String credentialNo;

        @Schema(description = "证件类型", example = "")
        @IntEnum(value = CredentialTypeEnum.class,message = "证件类型不正确")
        private Integer credentialType;

        @Mobile
        @Schema(description = "手机号", example = "")
        private String mobile;

        @Schema(description = "内部平台下单必传，用于区分一票多人券码对应关系", example = "")
        private String otaDetailId;

        @Schema(description = "是否投保 0否 1是", example = "")
        private Integer isInsure;
    }

    @Schema(description = "游客信息（一票多人）")
    @Data
    @Valid
    public static class MultilVisitor {

        @Schema(description = "游客列表", example = "")
        private List<Visitor> visitorList;

    }

    @Schema(description = "订单联系人")
    @Data
    @Valid
    public static class OrderUser{

        @Schema(description = "姓名", example = "")
        private String contactName;

        @Schema(description = "证件号", example = "")
        private String credentialNo;

        @Schema(description = "证件类型", example = "")
        @IntEnum(value = CredentialTypeEnum.class,message = "证件类型不正确")
        private Integer credentialType;

        @Mobile
        @Schema(description = "手机号", example = "")
        private String mobile;

        @Schema(description = "区/县", example = "")
        private String county;

        @Schema(description = "省", example = "")
        private String province;

        @Schema(description = "城市", example = "")
        private String city;

        @Schema(description = "客户", example = "")
        private String clientUser;

    }

    @Schema(description = "订单属性")
    @Data
    @Valid
    public static class OrderProperty{

        @Schema(description = "游客类型", example = "")
        private Integer visitorType;
        @Schema(description = "团队类型", example = "")
        private Integer teamType;
        @Schema(description = "销售渠道", example = "0")
        private Integer sellMethod;
        @Schema(description = "来源ID", example = "123")
        private String sourceId;
        @Schema(description = "来源名称", example = "美团")
        private String sourceName;
        @Schema(description = "会员ID", example = "123")
        private String memberId;
        @Schema(description = "销售员ID", example = "123")
        private String sellerId;
        @Schema(description = "门店ID", example = "123")
        private Long storeId;
        @Schema(description = "门店名称", example = "123")
        private String storeName;
        @Schema(description = "业绩共享人", example = "123")
        private String shareName;
        @Schema(description = "个人分销ID", example = "123")
        private Long personalDistributorId;
        @Schema(description = "优惠券码ID", example = "123")
        private Long couponCodeId;
        @Schema(description = "链接ID", example = "123")
        private Long linkId;
    }

    @Schema(description = "打包产品属性，目前指小程序产品打包")
    @Data
    @Valid
    public static class PackProduct{
        @Schema(description = "打包产品ID", example = "123")
        private Long packProductId;
        @Schema(description = "打包产品名称", example = "车+票")
        private String packProductName;
    }

    @Schema(description = "酒店入住信息,小程序下单")
    @Data
    @Valid
    public static class HotelBooking{

        @Schema(description = "房型", example = "")
        private Integer roomType;
        @Schema(description = "床型", example = "")
        private Integer bedType;
        @Schema(description = "含早类型", example = "")
        private Integer hasBreakfast;
        @Schema(description = "到店时间", example = "17:00")
        private String arriveTime;
        @Schema(description = "是否保留房间", example = "2")
        private Integer isKeepRoom;
        @Schema(description = "几晚", example = "2")
        private Integer nightDays;
        @Schema(description = "入住日期", example = "2025-06-01")
        private Date checkinTime;
        @Schema(description = "离店日期", example = "2025-06-01")
        private Date checkoutTime;
    }

    @Schema(description = "明细价格")
    @Data
    @Valid
    public static class DetailPrice{
        @Schema(description = "使用日期", example = "2025-06-01")
        private Date useDate;
        @Schema(description = "销售价", example = "10")
        private BigDecimal salePrice;
        @Schema(description = "原始金额", example = "10")
        private BigDecimal originPrice;
    }


    @Schema(description = "交付信息,小程序下单")
    @Data
    @Valid
    public static class DeliveryInfo{

        @Schema(description = "地址ID", example = "")
        private Long addressId;
        @Schema(description = "交付类型 0 邮寄 1自提", example = "")
        private Integer deliveryType;
        @Schema(description = "自提点ID", example = "")
        private Integer pickupId;
        @Schema(description = "自提点名称", example = "17:00")
        private String pickupName;
    }
}
