import 'package:sigap/src/features/panic-button/data/models/models/events_model.dart';

enum SessionStatus { active, inactive }

class SessionsModel {
  final String id;
  final String userId;
  final String eventId;
  final SessionStatus status;
  final DateTime createdAt;
  final EventModel event;

  SessionsModel({
    required this.id,
    required this.userId,
    required this.eventId,
    required this.status,
    required this.createdAt,
    required this.event,
  });

  // Create a SessionsModel instance from a JSON object
  factory SessionsModel.fromJson(Map<String, dynamic> json) {
    return SessionsModel(
      id: json['id'] as String,
      userId: json['user_id'] as String,
      eventId: json['event_id'] as String,
      status: SessionStatus.values.firstWhere(
        (e) => e.toString() == 'SessionStatus.${json['status']}',
      ),
      createdAt: DateTime.parse(json['created_at'] as String),
      event: EventModel.fromJson(json['event'] as Map<String, dynamic>),
    );
  }

  // Convert a SessionsModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'event_id': eventId,
      'status': status.toString().split('.').last,
      'created_at': createdAt.toIso8601String(),
      'event': event.toJson(),
    };
  }

  // Create a copy of the SessionsModel with updated fields
  SessionsModel copyWith({
    String? id,
    String? userId,
    String? eventId,
    SessionStatus? status,
    DateTime? createdAt,
    EventModel? event,
  }) {
    return SessionsModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      eventId: eventId ?? this.eventId,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      event: event ?? this.event,
    );
  }

  @override
  String toString() {
    return 'SessionsModel(id: $id, userId: $userId, eventId: $eventId, status: $status)';
  }
}
