import 'package:sigap/src/features/map/data/models/models/locations_model.dart';

class EventModel {
  final String id;
  final String name;
  final String? description;
  final String code;
  final DateTime createdAt;
  final String userId;
  final List<LocationModel>? locations;

  EventModel({
    required this.id,
    required this.name,
    this.description,
    required this.code,
    required this.createdAt,
    required this.userId,
    this.locations,
  });

  // Create an EventModel instance from a JSON object
  factory EventModel.fromJson(Map<String, dynamic> json) {
    return EventModel(
      id: json['id'] as String,
      name: json['name'] as String,
      description: json['description'] as String?,
      code: json['code'] as String,
      createdAt: DateTime.parse(json['created_at'] as String),
      userId: json['user_id'] as String,
      locations:
          json['locations'] != null
              ? (json['locations'] as List)
                  .map((e) => LocationModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
    );
  }

  // Convert an EventModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'code': code,
      'created_at': createdAt.toIso8601String(),
      'user_id': userId,
    };
  }

  // Create a copy of the EventModel with updated fields
  EventModel copyWith({
    String? id,
    String? name,
    String? description,
    String? code,
    DateTime? createdAt,
    String? userId,
    List<LocationModel>? locations,
  }) {
    return EventModel(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      code: code ?? this.code,
      createdAt: createdAt ?? this.createdAt,
      userId: userId ?? this.userId,
      locations: locations ?? this.locations,
    );
  }

  @override
  String toString() {
    return 'EventModel(id: $id, name: $name, code: $code)';
  }
}
