// 电商商城系统类型定义 - UTS Android 兼容

// 用户类型
export type UserType = {
	id: string
	phone: string
	email: string | null
	nickname: string | null
	avatar_url: string | null
	gender: number
	user_type: number
	status: number
	created_at: string
}

// 商城用户扩展信息类型
export type MallUserProfileType = {
	id: string
	user_id: string
	user_type: number
	status: number
	real_name: string | null
	id_card: string | null
	credit_score: number
	mall_role: string
	verification_status: number
	verification_data: UTSJSONObject | null
	business_license: string | null
	shop_category: string | null
	service_areas: UTSJSONObject | null
	emergency_contact: string | null
	preferences: UTSJSONObject | null
	created_at: string
	updated_at: string
}

// 用户地址类型
export type UserAddressType = {
	id: string
	user_id: string
	receiver_name: string
	receiver_phone: string
	province: string
	city: string
	district: string
	address_detail: string
	postal_code: string | null
	is_default: boolean
	label: string | null
	coordinates: string | null
	delivery_instructions: string | null
	business_hours: string | null
	status: number
	created_at: string
	updated_at: string
}

// 商家类型
export type MerchantType = {
	id: string
	user_id: string
	shop_name: string
	shop_logo: string | null
	shop_banner: string | null
	shop_description: string | null
	contact_name: string
	contact_phone: string
	shop_status: number
	rating: number
	total_sales: number
	created_at: string
}

// 商品类型
export type ProductType = {
	id: string
	merchant_id: string
	category_id: string
	name: string
	description: string | null
	images: Array<string>
	price: number
	original_price: number | null
	stock: number
	sales: number
	status: number
	created_at: string
	// 药品相关字段
	specification?: string | null  // 规格说明
	usage?: string | null          // 用法用量
	side_effects?: string | null   // 副作用
	precautions?: string | null    // 注意事项
	expiry_date?: string | null    // 有效期
	storage_conditions?: string | null // 储存条件
	approval_number?: string | null // 批准文号
	tags?: Array<string> | null    // 商品标签
}

// 商品SKU类型
export type ProductSkuType = {
	id: string
	product_id: string
	sku_code: string
	specifications: UTSJSONObject | null
	price: number
	stock: number
	image_url: string | null
	status: number
}

// 购物车类型
export type CartItemType = {
	id: string
	user_id: string
	product_id: string
	sku_id: string
	quantity: number
	selected: boolean
	product: ProductType | null
	sku: ProductSkuType | null
}

// 订单类型
export type OrderType = {
	id: string
	order_no: string
	user_id: string
	merchant_id: string
	status: number
	total_amount: number
	discount_amount: number
	delivery_fee: number
	actual_amount: number
	payment_method: number | null
	payment_status: number
	delivery_address: UTSJSONObject
	created_at: string
}

// 订单商品类型
export type OrderItemType = {
	id: string
	order_id: string
	product_id: string
	sku_id: string
	product_name: string
	sku_specifications: UTSJSONObject | null
	price: number
	quantity: number
	total_amount: number
}

// 配送员类型
export type DeliveryDriverType = {
	id: string
	user_id: string
	real_name: string
	id_card: string
	driver_license: string | null
	vehicle_type: number
	vehicle_number: string | null
	work_status: number
	current_location: UTSJSONObject | null
	service_areas: Array<string>
	rating: number
	total_orders: number
	auth_status: number
	created_at: string
	updated_at: string
}

// 配送任务类型
export type DeliveryTaskType = {
	id: string
	order_id: string
	driver_id: string | null
	pickup_address: UTSJSONObject
	delivery_address: UTSJSONObject
	distance: number | null
	estimated_time: number | null
	delivery_fee: number
	status: number
	pickup_time: string | null
	delivered_time: string | null
	delivery_code: string | null
	remark: string | null
	created_at: string
	updated_at: string
}

// 优惠券模板类型
export type CouponTemplateType = {
	id: string
	name: string
	description: string | null
	coupon_type: number
	discount_type: number
	discount_value: number
	min_order_amount: number
	max_discount_amount: number | null
	total_quantity: number | null
	per_user_limit: number
	usage_limit: number
	merchant_id: string | null
	category_ids: Array<string>
	product_ids: Array<string>
	user_type_limit: number | null
	start_time: string
	end_time: string
	status: number
	created_at: string
}

// 用户优惠券类型
export type UserCouponType = {
	id: string
	user_id: string
	template_id: string
	coupon_code: string
	status: number
	used_at: string | null
	order_id: string | null
	received_at: string
	expire_at: string
}

// 分页数据类型
export type PageDataType<T> = {
	data: Array<T>
	total: number
	page: number
	pageSize: number
	hasMore: boolean
}

// API响应类型
export type ApiResponseType<T> = {
	success: boolean
	data: T | null
	message: string
	code: number
}

// 订单状态枚举
export const ORDER_STATUS = {
	PENDING_PAYMENT: 1,
	PAID: 2,
	SHIPPED: 3,
	DELIVERED: 4,
	COMPLETED: 5,
	CANCELLED: 6,
	REFUNDING: 7,
	REFUNDED: 8
}

// 优惠券类型枚举
export const COUPON_TYPE = {
	DISCOUNT_AMOUNT: 1, // 满减券
	DISCOUNT_PERCENT: 2, // 折扣券
	FREE_SHIPPING: 3, // 免运费券
	NEWBIE: 4, // 新人券
	MEMBER: 5, // 会员券
	CATEGORY: 6, // 品类券
	MERCHANT: 7, // 商家券
	LIMITED_TIME: 8 // 限时券
}

// 支付方式枚举
export const PAYMENT_METHOD = {
	WECHAT: 1,
	ALIPAY: 2,
	UNIONPAY: 3,
	BALANCE: 4
}

// 配送状态枚举
export const DELIVERY_STATUS = {
	PENDING: 1,
	ASSIGNED: 2,
	PICKED_UP: 3,
	IN_TRANSIT: 4,
	DELIVERED: 5,
	FAILED: 6
}

// 用户类型枚举
export const MALL_USER_TYPE = {
	CONSUMER: 1, // 消费者
	MERCHANT: 2, // 商家
	DELIVERY: 3, // 配送员
	SERVICE: 4, // 客服
	ADMIN: 5 // 管理员
}

// 用户状态枚举
export const USER_STATUS = {
	NORMAL: 1, // 正常
	FROZEN: 2, // 冻结
	CANCELLED: 3, // 注销
	PENDING: 4 // 待审核
} as const

// 认证状态枚举
export const VERIFICATION_STATUS = {
	UNVERIFIED: 0, // 未认证
	VERIFIED: 1, // 已认证
	FAILED: 2 // 认证失败
}

// 地址标签枚举
export const ADDRESS_LABEL = {
	HOME: 'home', // 家
	OFFICE: 'office', // 公司
	SCHOOL: 'school', // 学校
	OTHER: 'other' // 其他
}

// 收藏类型枚举
export const FAVORITE_TYPE = {
	PRODUCT: 'product', // 商品
	SHOP: 'shop' // 店铺
}

// =========================
// 订阅相关类型与枚举
// =========================

// 订阅周期枚举
export const SUBSCRIPTION_PERIOD = {
	MONTHLY: 'monthly',
	YEARLY: 'yearly'
}

// 订阅状态枚举
export const SUBSCRIPTION_STATUS = {
	TRIAL: 'trial',
	ACTIVE: 'active',
	PAST_DUE: 'past_due',
	CANCELED: 'canceled',
	EXPIRED: 'expired'
}

// 软件订阅方案类型
export type SubscriptionPlanType = {
	id: string
	plan_code: string
	name: string
	description: string | null
	features: UTSJSONObject | null // { featureKey: description }
	price: number // 单位：元（或分，取决于后端；前端以显示为准）
	currency: string | null // 'CNY' | 'USD' ...
	billing_period: string // 'monthly' | 'yearly'
	trial_days: number | null
	is_active: boolean
	sort_order?: number | null
	created_at?: string
	updated_at?: string
}

// 用户订阅记录类型
export type UserSubscriptionType = {
	id: string
	user_id: string
	plan_id: string
	status: string
	start_date: string
	end_date: string | null
	next_billing_date: string | null
	auto_renew: boolean
	cancel_at_period_end?: boolean | null
	metadata?: UTSJSONObject | null
	created_at?: string
	updated_at?: string
}

// 用户基础信息类型 (兼容 pages/user/types.uts)
export type UserProfile = {
  id?: string;
  username: string;
  email: string;
  gender?: string;
  birthday?: string;
  height_cm?: number;
  weight_kg?: number;
  bio?: string;
  avatar_url?: string;
  preferred_language?: string;
	health_goal?: string;
	service_address?: string;
	emergency_contact?: string;
	chronic_notes?: string;
	care_preference?: string;
  role?: string;
  school_id?: string;
  grade_id?: string;
  class_id?: string;
  created_at?: string;
  updated_at?: string;
}

export type UserStats = {
  trainings: number;
  points: number;
  streak: number;
}

// 足迹项类型
export type FootprintItemType = {
  id: string
  name: string
  price: number
  original_price: number | null
  image: string
  sales: number
  shopId: string
  shopName: string
  viewTime: number
}

// =========================
// 积分相关类型
// =========================

// 签到记录类型
export type SigninRecordType = {
  id: string
  user_id: string
  signin_date: string
  points_earned: number
  bonus_points: number
  continuous_days: number
  created_at: string
}

// 签到结果类型
export type SigninResultType = {
  success: boolean
  points: number
  continuous_days: number
  bonus_points: number
  total_points: number
  message: string
}

// 积分兑换商品类型
export type PointProductType = {
  id: string
  name: string
  description: string | null
  image_url: string | null
  product_type: string
  points_required: number
  original_price: number | null
  stock: number
  status: number
  sort_order: number
  created_at: string
}

// 积分兑换记录类型
export type PointExchangeType = {
  id: string
  user_id: string
  product_id: string
  quantity: number
  points_used: number
  status: number
  tracking_no: string | null
  address_snapshot: UTSJSONObject | null
  created_at: string
  product: PointProductType | null
}

// 积分规则类型
export type PointRuleType = {
  id: string
  rule_type: string
  rule_name: string
  points: number
  description: string | null
  config: UTSJSONObject | null
  status: number
}

// 积分概览类型
export type PointsOverviewType = {
  current_points: number
  total_earned: number
  total_used: number
  expiring_points: number
  expiring_date: string | null
}

// =========================
// 评价相关类型
// =========================

// 商品评价类型（扩展）
export type ProductReviewType = {
  id: string
  user_id: string
  product_id: string
  order_id: string
  order_item_id: string | null
  rating: number
  content: string | null
  images: string[]
  videos: string[]
  tags: string[]
  is_anonymous: boolean
  like_count: number
  is_edited: boolean
  append_content: string | null
  append_at: string | null
  append_images: string[]
  reply: string | null
  reply_time: string | null
  created_at: string
  updated_at: string
  user_name: string | null
  user_avatar: string | null
  is_liked: boolean
}

// 评价统计类型
export type ReviewStatsType = {
  total_count: number
  avg_rating: number
  good_rate: number
  rating_distribution: Map<string, number>
  tags: ReviewTagType[]
}

// 评价标签类型
export type ReviewTagType = {
  name: string
  count: number
}

// 评价点赞类型
export type ReviewLikeType = {
  id: string
  review_id: string
  user_id: string
  created_at: string
}

// 评价举报类型
export type ReviewReportType = {
  id: string
  review_id: string
  user_id: string
  reason: string
  description: string | null
  status: number
  handle_result: string | null
  created_at: string
}

// 配送员评价类型
export type DeliveryRatingType = {
  id: string
  order_id: string
  delivery_user_id: string
  user_id: string
  rating: number
  content: string | null
  created_at: string
}

// 我的评价列表项类型
export type MyReviewItemType = {
  id: string
  product_id: string
  product_name: string
  product_image: string
  rating: number
  content: string | null
  images: string[]
  created_at: string
  can_append: boolean
  can_edit: boolean
}

// =========================
// 推销模式相关类型
// =========================

// 用户余额类型
export type UserBalanceType = {
  id: string
  user_id: string
  balance: number
  frozen_balance: number
  total_earned: number
  total_withdrawn: number
  updated_at: string
}

// 余额变动记录类型
export type BalanceRecordType = {
  id: string
  user_id: string
  type: string
  amount: number
  balance_before: number
  balance_after: number
  related_id: string | null
  description: string | null
  operator_id: string | null
  created_at: string
}

// 分享记录类型
export type ShareRecordType = {
  id: string
  user_id: string
  product_id: string
  order_id: string
  order_item_id: string | null
  share_code: string
  product_name: string
  product_image: string | null
  product_price: number
  required_count: number
  current_count: number
  status: number
  reward_amount: number | null
  created_at: string
  completed_at: string | null
  expired_at: string | null
}

// 二级购买记录类型
export type SecondaryPurchaseType = {
  id: string
  share_record_id: string
  buyer_id: string
  order_id: string
  quantity: number
  unit_price: number
  created_at: string
}

// 免单奖励记录类型
export type FreeOrderRewardType = {
  id: string
  user_id: string
  share_record_id: string
  amount: number
  status: number
  balance_record_id: string | null
  cleared_at: string | null
  cleared_by: string | null
  created_at: string
}

// 会员等级类型
export type MemberLevelType = {
  id: number
  name: string
  min_amount: number
  discount: number
  icon: string | null
  description: string | null
  sort_order: number
  status: number
}

// 用户会员信息类型
export type UserMemberInfoType = {
  member_level: number
  level_name: string
  discount: number
  total_spent: number
  next_level: MemberLevelType | null
  progress_percent: number
  manual_level: boolean
}

// 会员等级变更记录类型
export type MemberLevelLogType = {
  id: string
  user_id: string
  old_level: number
  new_level: number
  reason: string | null
  operator_id: string | null
  created_at: string
}
