import 'package:sigap/src/features/personalization/data/models/models/users_model.dart';

class LocationLogModel {
  final String id;
  final String userId;
  final double latitude;
  final double longitude;
  final DateTime timestamp;
  final String? description;
  final DateTime createdAt;
  final DateTime updatedAt;
  final UserModel? user;

  LocationLogModel({
    required this.id,
    required this.userId,
    required this.latitude,
    required this.longitude,
    required this.timestamp,
    this.description,
    required this.createdAt,
    required this.updatedAt,
    this.user,
  });

  // Create a LocationLogModel instance from a JSON object
  factory LocationLogModel.fromJson(Map<String, dynamic> json) {
    return LocationLogModel(
      id: json['id'] as String,
      userId: json['user_id'] as String,
      latitude: json['latitude'].toDouble(),
      longitude: json['longitude'].toDouble(),
      timestamp: DateTime.parse(json['timestamp'] as String),
      description: json['description'] as String?,
      createdAt: DateTime.parse(json['created_at'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String),
      user:
          json['users'] != null
              ? UserModel.fromJson(json['users'] as Map<String, dynamic>)
              : null,
    );
  }

  // Convert a LocationLogModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'latitude': latitude,
      'longitude': longitude,
      'timestamp': timestamp.toIso8601String(),
      'description': description,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  // Create a copy of the LocationLogModel with updated fields
  LocationLogModel copyWith({
    String? id,
    String? userId,
    double? latitude,
    double? longitude,
    DateTime? timestamp,
    String? description,
    DateTime? createdAt,
    DateTime? updatedAt,
    UserModel? user,
  }) {
    return LocationLogModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      latitude: latitude ?? this.latitude,
      longitude: longitude ?? this.longitude,
      timestamp: timestamp ?? this.timestamp,
      description: description ?? this.description,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      user: user ?? this.user,
    );
  }

  @override
  String toString() {
    return 'LocationLogModel(id: $id, userId: $userId, timestamp: $timestamp)';
  }
}
