import 'package:intl/intl.dart';

class AttendanceLogViewModel {
  final DateTime? activityTime;
  final String? imageUrl;
  final String? comment;
  final String? thumbPreSignedUrl;
  final String? preSignedUrl;
  final String? longitude;
  final String? latitude;
  final int? activity;

  AttendanceLogViewModel({
    this.activityTime,
    this.imageUrl,
    this.comment,
    this.thumbPreSignedUrl,
    this.preSignedUrl,
    this.longitude,
    this.latitude,
    required this.activity,
  });

  factory AttendanceLogViewModel.fromJson(Map<String, dynamic> json) {
    return AttendanceLogViewModel(
      activityTime: json['activityTime'] != null
          ? DateTime.tryParse(json['activityTime'])
          : null,
      imageUrl: json['imageUrl']?.toString(),
      comment: json['comment']?.toString(),
      thumbPreSignedUrl: json['thumbPreSignedUrl']?.toString(),
      preSignedUrl: json['preSignedUrl']?.toString(),
      longitude: json['longitude']?.toString(),
      latitude: json['latitude']?.toString(),
      activity: json['activity'] ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'activityTime': activityTime?.toIso8601String(),
      'imageUrl': imageUrl,
      'comment': comment,
      'thumbPreSignedUrl': thumbPreSignedUrl,
      'preSignedUrl': preSignedUrl,
      'longitude': longitude,
      'latitude': latitude,
      'activity': activity,
    };
  }

  String? get formattedDate => activityTime != null
      ? DateFormat('yyyy-MM-dd').format(activityTime!)
      : null;

  String? get formattedTime =>
      activityTime != null ? DateFormat('hh:mm a').format(activityTime!) : null;
}
