import 'package:autosos_flutter/model/payment.dart';

/// 七牛上传凭证（对应 Android QiNiuEntity）
class QiNiuToken {
  final String token;
  final String domain;        // 上传后返回的访问域名
  final int expireAt;         // 过期时间戳

  const QiNiuToken({
    required this.token,
    required this.domain,
    required this.expireAt,
  });

  factory QiNiuToken.fromJson(Map<String, dynamic> json) => QiNiuToken(
        token: json['token'] as String? ?? '',
        domain: json['domain'] as String? ?? '',
        expireAt: json['expire_at'] as int? ?? 0,
      );
}

/// 二维码（对应 Android WxEwmEntity）
class QrCode {
  final String url;
  final String? scene;
  final int expireAt;

  const QrCode({
    required this.url,
    this.scene,
    required this.expireAt,
  });

  factory QrCode.fromJson(Map<String, dynamic> json) => QrCode(
        url: json['url'] as String? ?? '',
        scene: json['scene'] as String?,
        expireAt: json['expire_at'] as int? ?? 0,
      );
}

/// 验证码发送结果（对应 Android BindPhoneEntity）
class SmsResult {
  final bool sent;
  final int cooldown;     // 倒计时秒数

  const SmsResult({required this.sent, required this.cooldown});

  factory SmsResult.fromJson(Map<String, dynamic> json) => SmsResult(
        sent: json['sent'] as bool? ?? false,
        cooldown: json['cooldown'] as int? ?? 60,
      );
}

/// 服务类型（对应 Android ServiceType）
class ServiceType {
  final int id;
  final String name;
  final String? icon;
  final double basePrice;

  const ServiceType({
    required this.id,
    required this.name,
    this.icon,
    required this.basePrice,
  });

  factory ServiceType.fromJson(Map<String, dynamic> json) => ServiceType(
        id: json['id'] as int? ?? 0,
        name: json['name'] as String? ?? '',
        icon: json['icon'] as String?,
        basePrice: (json['base_price'] as num?)?.toDouble() ?? 0,
      );
}

/// 消息中心项（对应 Android MessengerItemViewModel）
class MessengerItem {
  final int id;
  final String title;
  final String content;
  final DateTime createdAt;
  final bool read;
  final String? link;

  const MessengerItem({
    required this.id,
    required this.title,
    required this.content,
    required this.createdAt,
    this.read = false,
    this.link,
  });

  factory MessengerItem.fromJson(Map<String, dynamic> json) => MessengerItem(
        id: json['id'] as int? ?? 0,
        title: json['title'] as String? ?? '',
        content: json['content'] as String? ?? '',
        createdAt: DateTime.tryParse(json['created_at'] as String? ?? '') ??
            DateTime.now(),
        read: json['read'] as bool? ?? false,
        link: json['link'] as String?,
      );
}

/// 应用更新信息（对应 Android UpdateStatusRes）
class AppUpdate {
  final bool forceUpdate;       // 是否强制
  final String latestVersion;   // 最新版本号
  final String? downloadUrl;    // APK 下载地址
  final String? changelog;      // 更新日志
  final int versionCode;

  const AppUpdate({
    required this.forceUpdate,
    required this.latestVersion,
    required this.versionCode,
    this.downloadUrl,
    this.changelog,
  });

  factory AppUpdate.fromJson(Map<String, dynamic> json) => AppUpdate(
        forceUpdate: json['force_update'] as bool? ?? false,
        latestVersion: json['latest_version'] as String? ?? '',
        downloadUrl: json['download_url'] as String?,
        changelog: json['changelog'] as String?,
        versionCode: json['version_code'] as int? ?? 0,
      );
}

/// 反馈（对应 Android Feedback）
class Feedback {
  final int id;
  final String content;
  final String? contact;
  final List<String>? images;
  final DateTime createdAt;

  const Feedback({
    required this.id,
    required this.content,
    required this.createdAt,
    this.contact,
    this.images,
  });

  factory Feedback.fromJson(Map<String, dynamic> json) => Feedback(
        id: json['id'] as int? ?? 0,
        content: json['content'] as String? ?? '',
        contact: json['contact'] as String?,
        images: (json['images'] as List?)?.cast<String>(),
        createdAt: DateTime.tryParse(json['created_at'] as String? ?? '') ??
            DateTime.now(),
      );
}

/// 完成订单详情（对应 Android FinishOrderEntity / CompleteDetails）
class CompleteOrderInfo {
  final int orderId;
  final double totalAmount;
  final double? discount;
  final double? serviceFee;
  final String? remark;
  final List<AmountItem> amountItems;

  const CompleteOrderInfo({
    required this.orderId,
    required this.totalAmount,
    this.discount,
    this.serviceFee,
    this.remark,
    this.amountItems = const [],
  });

  factory CompleteOrderInfo.fromJson(Map<String, dynamic> json) =>
      CompleteOrderInfo(
        orderId: json['order_id'] as int? ?? 0,
        totalAmount: (json['total_amount'] as num?)?.toDouble() ?? 0,
        discount: (json['discount'] as num?)?.toDouble(),
        serviceFee: (json['service_fee'] as num?)?.toDouble(),
        remark: json['remark'] as String?,
        amountItems: ((json['amount_items'] as List?) ?? [])
            .map((e) => AmountItem.fromJson(e as Map<String, dynamic>))
            .toList(),
      );
}

/// 救援轨迹点（用于 AMap Track 录制）
class TrackPoint {
  final double lat;
  final double lng;
  final DateTime time;
  final double? speed;
  final double? bearing;

  const TrackPoint({
    required this.lat,
    required this.lng,
    required this.time,
    this.speed,
    this.bearing,
  });

  Map<String, dynamic> toJson() => {
        'lat': lat,
        'lng': lng,
        'time': time.toIso8601String(),
        'speed': speed,
        'bearing': bearing,
      };

  factory TrackPoint.fromJson(Map<String, dynamic> json) => TrackPoint(
        lat: (json['lat'] as num?)?.toDouble() ?? 0,
        lng: (json['lng'] as num?)?.toDouble() ?? 0,
        time: DateTime.tryParse(json['time'] as String? ?? '') ??
            DateTime.now(),
        speed: (json['speed'] as num?)?.toDouble(),
        bearing: (json['bearing'] as num?)?.toDouble(),
      );
}