import 'dart:convert';

SavePresensiResponseModel savePresensiResponseModelFromJson(String str) =>
    SavePresensiResponseModel.fromJson(json.decode(str));

String savePresensiResponseModelToJson(SavePresensiResponseModel data) =>
    json.encode(data.toJson());

class SavePresensiResponseModel {
  SavePresensiResponseModel({
    required this.success,
    required this.message,
    this.data,
  });

  bool success;
  String message;
  Data? data; // Nullable data

  factory SavePresensiResponseModel.fromJson(Map<String, dynamic> json) =>
      SavePresensiResponseModel(
        success: json["success"] ?? false,
        message: json["message"] ?? "",
        data: json["data"] != null ? Data.fromJson(json["data"]) : null,
      );

  Map<String, dynamic> toJson() => {
        "success": success,
        "message": message,
        "data": data?.toJson(), // Handle nullable data
      };
}

class Data {
  Data({
    required this.id,
    required this.userId,
    required this.latitude,
    required this.longitude,
    required this.tanggal,
    required this.masuk,
    this.pulang,
    required this.createdAt,
    required this.updatedAt,
    required this.status,
    required this.keterangan,
    this.foto,
  });

  String id; // Ubah menjadi String
  String userId; // Ubah menjadi String jika perlu
  String latitude;
  String longitude;
  DateTime tanggal;
  String masuk;
  String? pulang;
  DateTime createdAt;
  DateTime updatedAt;
  String status;
  String keterangan;
  String? foto;

  factory Data.fromJson(Map<String, dynamic> json) => Data(
        id: json["id"].toString(), // Konversi ke String
        userId: json["user_id"].toString(), // Konversi ke String jika perlu
        latitude: json["latitude"] ?? "",
        longitude: json["longitude"] ?? "",
        tanggal: json["tanggal"] != null
            ? DateTime.parse(json["tanggal"])
            : DateTime.now(),
        masuk: json["masuk"] ?? "",
        pulang: json["pulang"],
        createdAt: json["created_at"] != null
            ? DateTime.parse(json["created_at"])
            : DateTime.now(),
        updatedAt: json["updated_at"] != null
            ? DateTime.parse(json["updated_at"])
            : DateTime.now(),
        status: json['status'] ?? "",
        keterangan: json['keterangan'] ?? "",
        foto: json['foto'],
      );

  Map<String, dynamic> toJson() => {
        "id": id, // Tetap sebagai String
        "user_id": userId, // Tetap sebagai String jika perlu
        "latitude": latitude,
        "longitude": longitude,
        "tanggal": tanggal.toIso8601String(),
        "masuk": masuk,
        "pulang": pulang,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "status": status,
        "keterangan": keterangan,
        "foto": foto,
      };
}
