import 'package:cloud_firestore/cloud_firestore.dart';

class EventTypeModel {
  final String id;
  final String name;
  final double defaultPrice;
  final DateTime createdAt;

  EventTypeModel({
    required this.id,
    required this.name,
    required this.defaultPrice,
    required this.createdAt,
  });

  factory EventTypeModel.fromMap(Map<String, dynamic> map, String id) {
    // Gérer createdAt qui peut être Timestamp (Firestore) ou String ISO (API)
    DateTime parseCreatedAt(dynamic value) {
      if (value == null) return DateTime.now();
      if (value is Timestamp) return value.toDate();
      if (value is String) return DateTime.tryParse(value) ?? DateTime.now();
      return DateTime.now();
    }

    return EventTypeModel(
      id: id,
      name: map['name'] ?? '',
      defaultPrice: (map['defaultPrice'] ?? 0.0).toDouble(),
      createdAt: parseCreatedAt(map['createdAt']),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'defaultPrice': defaultPrice,
      'createdAt': createdAt,
    };
  }
}
