/**
 * 生成订单号: 时间戳 + 6位随机数
 */
export function generateOrderNo(): string {
  const now = new Date();
  const ts = [
    now.getFullYear(),
    String(now.getMonth() + 1).padStart(2, '0'),
    String(now.getDate()).padStart(2, '0'),
    String(now.getHours()).padStart(2, '0'),
    String(now.getMinutes()).padStart(2, '0'),
    String(now.getSeconds()).padStart(2, '0'),
  ].join('');
  const rand = String(Math.floor(Math.random() * 1000000)).padStart(6, '0');
  return ts + rand;
}

/**
 * 计算入住晚数
 */
export function calcNights(checkIn: string, checkOut: string): number {
  const inDate = new Date(checkIn);
  const outDate = new Date(checkOut);
  const diff = outDate.getTime() - inDate.getTime();
  return Math.max(1, Math.ceil(diff / (1000 * 60 * 60 * 24)));
}

/**
 * 手机号脱敏
 */
export function maskPhone(phone: string): string {
  if (!phone || phone.length < 11) return phone;
  return phone.slice(0, 3) + '****' + phone.slice(7);
}

/**
 * 分页参数校验与默认值
 */
export function normalizePagination(page?: number, pageSize?: number): { page: number; pageSize: number; offset: number } {
  const p = Math.max(1, Number(page) || 1);
  const ps = Math.min(100, Math.max(1, Number(pageSize) || 10));
  return { page: p, pageSize: ps, offset: (p - 1) * ps };
}

/**
 * 价格计算: 房费 - 优惠券
 */
export function calcOrderAmount(roomPrice: number, nights: number, couponDiscount: number): { totalAmount: number; payAmount: number } {
  const totalAmount = roomPrice * nights;
  const payAmount = Math.max(0, totalAmount - couponDiscount);
  return { totalAmount, payAmount };
}

// ===================== 导出格式化工具 =====================

export * from './format';
