class Login {
  int? code;
  String? message;
  bool? status;

  int? userid;
  String? contactno;
  String? devicetype;
  int? configid;
  String? deviceid;
  String? userfcmtoken;
  String? authname;
  int? authmode;
  int? roleid;
  String? firstname;
  String? lastname;
  String? fullname;
  String? password;
  String? email;
  String? address;
  String? suburb;
  String? city;
  String? state;
  String? postcode;
  int? pin;
  int? shiftid;
  int? logid;
  int? partnerid;
  int? tenantid;
  int? riderid;
  int? deliveryradius;
  int? locationid;
  int? applocationid;

  Login({
    this.code,
    this.message,
    this.status,
    this.userid,
    this.contactno,
    this.devicetype,
    this.configid,
    this.deviceid,
    this.userfcmtoken,
    this.authname,
    this.authmode,
    this.roleid,
    this.firstname,
    this.lastname,
    this.fullname,
    this.password,
    this.email,
    this.address,
    this.suburb,
    this.city,
    this.state,
    this.postcode,
    this.pin,
    this.shiftid,
    this.logid,
    this.partnerid,
    this.tenantid,
    this.riderid,
    this.deliveryradius,
    this.locationid,
    this.applocationid,
  });

  /// Factory: builds a Login model from JSON (supports both "data" and "details")
  factory Login.fromJson(Map<String, dynamic> json) {
    // Safely extract nested data from either "data" or "details"
    final Map<String, dynamic> nested =
        (json['details'] ?? json['data'] ?? <String, dynamic>{});
    
    final dynamic topAuthMode = json['authmode'];
    final dynamic nestedAuthMode = nested['authmode'];

    return Login(
      code: json['code'] is int
          ? json['code']
          : int.tryParse('${json['code']}'),
      message: json['message']?.toString(),
      status: json['status'] is bool ? json['status'] : json['status'] == 1,
      userid: json['userid'] ?? nested['userid'],
      contactno:
          json['contactno']?.toString() ?? nested['contactno']?.toString(),
      devicetype: json['devicetype']?.toString(),
      configid: json['configid'] ?? nested['configid'],
      deviceid: json['deviceid']?.toString(),
      userfcmtoken:
          json['userfcmtoken']?.toString() ?? nested['userfcmtoken']?.toString(),
      authname: (json['authname'] ?? nested['authname'])?.toString(),
      authmode: topAuthMode is int
          ? topAuthMode
          : (nestedAuthMode is int
                ? nestedAuthMode
                : int.tryParse('$topAuthMode')),
      roleid: nested['roleid'],
      firstname: nested['firstname']?.toString(),
      lastname: nested['lastname']?.toString(),
      fullname: nested['fullname']?.toString(),
      password: nested['password']?.toString(),
      email: nested['email']?.toString(),
      address: nested['address']?.toString(),
      suburb: nested['suburb']?.toString(),
      city: nested['city']?.toString(),
      state: nested['state']?.toString(),
      postcode: nested['postcode']?.toString(),
      pin: nested['pin'],
      shiftid: nested['shiftid'],
      logid: nested['logid'],
      partnerid: nested['partnerid'],
      tenantid: nested['tenantid'],
      riderid: nested['riderid'],
      deliveryradius: nested['deliveryradius'],
      locationid: nested['locationid'] is int
          ? nested['locationid']
          : int.tryParse('${nested['locationid'] ?? 0}'),
      applocationid: nested['applocationid'] is int
          ? nested['applocationid']
          : int.tryParse('${nested['applocationid'] ?? 0}'),
    );
  }

  /// Convert this model to JSON for sending to the backend
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (contactno != null) data['contactno'] = contactno;
    if (devicetype != null) data['devicetype'] = devicetype;
    if (configid != null) data['configid'] = configid;
    if (deviceid != null) data['deviceid'] = deviceid;
    if (userfcmtoken != null) data['userfcmtoken'] = userfcmtoken;
    if (pin != null) data['pin'] = pin;
    return data;
  }
}
