class PriceData {
  final String id;
  final String platform;
  final String sellerName;
  final double pricePerMillion;
  final DateTime timestamp;
  final String? url;

  PriceData({
    required this.id,
    required this.platform,
    required this.sellerName,
    required this.pricePerMillion,
    required this.timestamp,
    this.url,
  });

  factory PriceData.fromJson(Map<String, dynamic> json) {
    return PriceData(
      id: json['id'] as String,
      platform: json['platform'] as String,
      sellerName: (json['sellerName'] as String?) ?? 'Unknown',
      pricePerMillion: (json['pricePerMillion'] as num).toDouble(),
      timestamp: DateTime.parse(json['timestamp'] as String),
      url: json['url'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'platform': platform,
      'sellerName': sellerName,
      'pricePerMillion': pricePerMillion,
      'timestamp': timestamp.toIso8601String(),
      'url': url,
    };
  }
}

class LatestPrice {
  final double lowestPrice;
  final String sellerName;
  final String platform;
  final List<PriceData> allPrices;
  final DateTime timestamp;

  LatestPrice({
    required this.lowestPrice,
    required this.sellerName,
    required this.platform,
    required this.allPrices,
    required this.timestamp,
  });

  factory LatestPrice.fromJson(Map<String, dynamic> json) {
    final data = json['data'] as Map<String, dynamic>;
    return LatestPrice(
      lowestPrice: (data['lowestPrice'] as num).toDouble(),
      sellerName: data['sellerName'] as String,
      platform: data['platform'] as String,
      allPrices: (data['allPrices'] as List)
          .map((e) => PriceData.fromJson(e as Map<String, dynamic>))
          .toList(),
      timestamp: DateTime.parse(data['timestamp'] as String),
    );
  }
}

class HistoryData {
  final List<HistoryPrice> prices;

  HistoryData({required this.prices});

  factory HistoryData.fromJson(Map<String, dynamic> json) {
    return HistoryData(
      prices: (json['data'] as List)
          .map((e) => HistoryPrice.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }
}

class HistoryPrice {
  final DateTime timestamp;
  final double price;
  final String vendor;
  final String seller;

  HistoryPrice({
    required this.timestamp,
    required this.price,
    required this.vendor,
    required this.seller,
  });

  factory HistoryPrice.fromJson(Map<String, dynamic> json) {
    return HistoryPrice(
      timestamp: DateTime.parse(json['timestamp'] as String),
      price: double.parse(json['price'] as String),
      vendor: json['vendor'] as String,
      seller: json['seller'] as String,
    );
  }

  // Convert to PriceData for compatibility
  PriceData toPriceData() {
    return PriceData(
      id: '${timestamp.millisecondsSinceEpoch}-$seller',
      platform: vendor,
      sellerName: seller,
      pricePerMillion: price,
      timestamp: timestamp,
    );
  }
}

class PriceAlert {
  final String id;
  final double auecAmount;
  final double maxPrice;
  final bool enabled;

  PriceAlert({
    required this.id,
    required this.auecAmount,
    required this.maxPrice,
    required this.enabled,
  });

  factory PriceAlert.fromJson(Map<String, dynamic> json) {
    return PriceAlert(
      id: json['id'] as String,
      auecAmount: (json['auecAmount'] as num).toDouble(),
      maxPrice: (json['maxPrice'] as num).toDouble(),
      enabled: json['enabled'] as bool,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'auecAmount': auecAmount,
      'maxPrice': maxPrice,
      'enabled': enabled,
    };
  }

  PriceAlert copyWith({
    String? id,
    double? auecAmount,
    double? maxPrice,
    bool? enabled,
  }) {
    return PriceAlert(
      id: id ?? this.id,
      auecAmount: auecAmount ?? this.auecAmount,
      maxPrice: maxPrice ?? this.maxPrice,
      enabled: enabled ?? this.enabled,
    );
  }
}
