package dto

import "time"

type CreateOrderRequest struct {
	OrderType     string `json:"order_type" validate:"required,oneof=single_card device" required:"true" description:"订单类型 (single_card:单卡购买, device:设备购买)"`
	IotCardID     *uint  `json:"iot_card_id" validate:"required_if=OrderType single_card" description:"IoT卡ID(单卡购买时必填)"`
	DeviceID      *uint  `json:"device_id" validate:"required_if=OrderType device" description:"设备ID(设备购买时必填)"`
	PackageIDs    []uint `json:"package_ids" validate:"required,min=1,max=10,dive,min=1" required:"true" minItems:"1" maxItems:"10" description:"套餐ID列表"`
	PaymentMethod string `json:"payment_method" validate:"required,oneof=wallet wechat alipay" required:"true" description:"支付方式 (wallet:钱包支付, wechat:微信支付, alipay:支付宝支付)"`
}

// CreateAdminOrderRequest 后台订单创建请求（仅允许 wallet/offline）
type CreateAdminOrderRequest struct {
	Identifier        string   `json:"identifier" validate:"required,min=1,max=100" required:"true" minLength:"1" maxLength:"100" description:"资产标识符（卡支持 ICCID，设备支持 VirtualNo、IMEI 或 SN）"`
	PackageIDs        []uint   `json:"package_ids" validate:"required,min=1,max=10,dive,min=1" required:"true" minItems:"1" maxItems:"10" description:"套餐ID列表"`
	PaymentMethod     string   `json:"payment_method" validate:"required,oneof=wallet offline" required:"true" description:"支付方式 (wallet:钱包支付, offline:线下支付)"`
	PaymentVoucherKey []string `json:"payment_voucher_key" validate:"omitempty,max=5,dive,max=500" maxItems:"5" description:"线下支付凭证对象存储file_key列表（payment_method=offline时至少1个，最多5个，通过/storage/upload-url上传图片后获得）"`
}

type OrderListRequest struct {
	Page          int        `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
	PageSize      int        `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
	PaymentStatus *int       `json:"payment_status" query:"payment_status" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"支付状态 (1:待支付, 2:已支付, 3:已取消, 4:已退款)"`
	PaymentMethod string     `json:"payment_method" query:"payment_method" validate:"omitempty,oneof=wallet wechat alipay offline" description:"支付方式 (wallet:钱包支付, wechat:微信支付, alipay:支付宝支付, offline:线下支付)"`
	OrderType     string     `json:"order_type" query:"order_type" validate:"omitempty,oneof=single_card device" description:"订单类型 (single_card:单卡购买, device:设备购买)"`
	SellerShopID  *uint      `json:"seller_shop_id" query:"seller_shop_id" validate:"omitempty,min=1" minimum:"1" description:"所属代理商ID（销售来源店铺ID）"`
	OrderNo       string     `json:"order_no" query:"order_no" validate:"omitempty,max=30" maxLength:"30" description:"订单号(精确查询)"`
	PurchaseRole  string     `json:"purchase_role" query:"purchase_role" validate:"omitempty,oneof=self_purchase purchased_by_parent purchased_by_platform purchase_for_subordinate" description:"订单角色 (self_purchase:自己购买, purchased_by_parent:上级代理购买, purchased_by_platform:平台代购, purchase_for_subordinate:给下级购买)"`
	StartTime     *time.Time `json:"start_time" query:"start_time" description:"创建时间起始"`
	EndTime       *time.Time `json:"end_time" query:"end_time" description:"创建时间结束"`
	IsExpired     *bool      `json:"is_expired" query:"is_expired" description:"是否已过期 (true:已过期, false:未过期)"`
	Identifier    string     `json:"identifier" query:"identifier" validate:"omitempty,max=100" maxLength:"100" description:"资产标识符（支持 ICCID/VirtualNo/IMEI/SN/MSISDN，按资产解析后查询对应订单）"`
	BuyerPhone    string     `json:"buyer_phone" query:"buyer_phone" validate:"omitempty,max=20" maxLength:"20" description:"买家手机号精确查询"`
}

type PayOrderRequest struct {
	PaymentMethod string `json:"payment_method" validate:"required,oneof=wallet wechat alipay" required:"true" description:"支付方式 (wallet:钱包支付, wechat:微信支付, alipay:支付宝支付)"`
}

type OrderItemResponse struct {
	ID          uint    `json:"id" description:"明细ID"`
	PackageID   uint    `json:"package_id" description:"套餐ID"`
	PackageName string  `json:"package_name" description:"套餐名称"`
	PackageType *string `json:"package_type" description:"套餐类型快照 (formal:正式套餐, addon:加油包)"`
	Quantity    int     `json:"quantity" description:"数量"`
	UnitPrice   int64   `json:"unit_price" description:"单价(分)"`
	Amount      int64   `json:"amount" description:"小计金额(分)"`
}

type OrderResponse struct {
	ID                      uint       `json:"id" description:"订单ID"`
	OrderNo                 string     `json:"order_no" description:"订单号"`
	OrderType               string     `json:"order_type" description:"订单类型 (single_card:单卡购买, device:设备购买)"`
	BuyerType               string     `json:"buyer_type" description:"买家类型 (personal:个人客户, agent:代理商)"`
	BuyerID                 uint       `json:"buyer_id" description:"买家ID"`
	BuyerPhone              *string    `json:"buyer_phone" description:"买家手机号快照（个人客户下单时记录）"`
	BuyerNickname           *string    `json:"buyer_nickname" description:"买家昵称快照（个人客户下单时记录）"`
	IotCardID               *uint      `json:"iot_card_id,omitempty" description:"IoT卡ID"`
	DeviceID                *uint      `json:"device_id,omitempty" description:"设备ID"`
	TotalAmount             int64      `json:"total_amount" description:"订单总金额(分)"`
	PaymentMethod           string     `json:"payment_method,omitempty" description:"支付方式 (wallet:钱包支付, wechat:微信支付, alipay:支付宝支付, offline:线下支付)"`
	PaymentStatus           int        `json:"payment_status" description:"支付状态 (1:待支付, 2:已支付, 3:已取消, 4:已退款)"`
	PaymentStatusText       string     `json:"payment_status_text" description:"支付状态文本"`
	PaidAt                  *time.Time `json:"paid_at,omitempty" description:"支付时间"`
	PaymentVoucherKey       []string   `json:"payment_voucher_key" description:"线下支付凭证对象存储file_key列表（线下支付订单有值，最多5个）"`
	IsPurchaseOnBehalf      bool       `json:"is_purchase_on_behalf" description:"是否为代购订单"`
	CommissionStatus        int        `json:"commission_status" description:"佣金流程状态 (1:待计算, 2:已完成, 3:待人工处理)"`
	CommissionStatusName    string     `json:"commission_status_name" description:"佣金流程状态名称（中文）"`
	CommissionResult        int        `json:"commission_result" description:"佣金业务结果 (0:未知, 1:有佣金, 2:无佣金, 3:链路异常)"`
	CommissionResultName    string     `json:"commission_result_name" description:"佣金业务结果名称（中文）"`
	CommissionConfigVersion int        `json:"commission_config_version" description:"佣金配置版本"`
	SellerShopID            *uint      `json:"seller_shop_id,omitempty" description:"销售店铺ID（业务归属字段）"`
	SellerShopName          string     `json:"seller_shop_name,omitempty" description:"销售店铺名称（业务归属展示）"`

	// 操作者信息
	OperatorID       *uint  `json:"operator_id,omitempty" description:"操作者账号ID（真实发起操作的账号/主体ID）"`
	OperatorType     string `json:"operator_type,omitempty" description:"操作者账号类型 (platform:平台, agent:代理, enterprise:企业, personal_customer:个人客户)"`
	OperatorName     string `json:"operator_name,omitempty" description:"操作者名称（按账号语义展示）"`
	ActualPaidAmount *int64 `json:"actual_paid_amount,omitempty" description:"实际支付金额(分)"`

	// 订单角色
	PurchaseRole        string `json:"purchase_role,omitempty" description:"订单角色 (self_purchase:自己购买, purchased_by_parent:上级代理购买, purchased_by_platform:平台代购, purchase_for_subordinate:给下级购买)"`
	IsPurchasedByParent bool   `json:"is_purchased_by_parent" description:"是否由上级代理购买"`
	PurchaseRemark      string `json:"purchase_remark,omitempty" description:"购买备注"`

	Items     []*OrderItemResponse `json:"items" description:"订单明细列表"`
	CreatedAt time.Time            `json:"created_at" description:"创建时间"`
	UpdatedAt time.Time            `json:"updated_at" description:"更新时间"`

	// 订单超时信息
	ExpiresAt *time.Time `json:"expires_at,omitempty" description:"订单过期时间"`
	IsExpired bool       `json:"is_expired" description:"是否已过期"`

	// 资产标识符快照
	AssetIdentifier string `json:"asset_identifier,omitempty" description:"下单时资产的标识符快照（卡为 ICCID，设备优先使用 VirtualNo，缺失时使用 IMEI）"`
	AssetType       string `json:"asset_type,omitempty" description:"资产类型 (card:单卡, device:设备)"`
}

type OrderListResponse struct {
	List     []*OrderResponse `json:"items" description:"订单列表"`
	Total    int64            `json:"total" description:"总数"`
	Page     int              `json:"page" description:"当前页码"`
	PageSize int              `json:"size" description:"每页数量"`
}

type GetOrderRequest struct {
	ID uint `path:"id" description:"订单ID" required:"true"`
}

type CancelOrderRequest struct {
	ID uint `path:"id" description:"订单ID" required:"true"`
}

type PayOrderParams struct {
	ID uint `path:"id" description:"订单ID" required:"true"`
	PayOrderRequest
}

type PurchaseCheckRequest struct {
	OrderType  string `json:"order_type" validate:"required,oneof=single_card device" required:"true" description:"订单类型 (single_card:单卡购买, device:设备购买)"`
	ResourceID uint   `json:"resource_id" validate:"required,min=1" required:"true" description:"资源ID (IoT卡ID或设备ID)"`
	PackageIDs []uint `json:"package_ids" validate:"required,min=1,max=10,dive,min=1" required:"true" minItems:"1" maxItems:"10" description:"套餐ID列表"`
}

type PurchaseCheckResponse struct {
	TotalPackageAmount  int64  `json:"total_package_amount" description:"套餐总价(分)"`
	NeedForceRecharge   bool   `json:"need_force_recharge" description:"是否需要强充"`
	ForceRechargeAmount int64  `json:"force_recharge_amount" description:"强充金额(分)"`
	ActualPayment       int64  `json:"actual_payment" description:"实际支付金额(分)"`
	WalletCredit        int64  `json:"wallet_credit" description:"钱包到账金额(分)"`
	Message             string `json:"message" description:"提示信息"`
}
