class AddressResponse {
  const AddressResponse({
    required this.id,
    required this.userId,
    required this.label,
    required this.street,
    required this.colonia,
    required this.routeId,
    required this.verified,
    this.verifiedMethod,
    this.verifiedAt,
    this.createdAt,
  });

  final String id;
  final String userId;
  final String label;
  final String street;
  final String colonia;
  final String routeId;
  final bool verified;
  final String? verifiedMethod;
  final String? verifiedAt;
  final String? createdAt;

  factory AddressResponse.fromJson(Map<String, dynamic> json) {
    final payload = json['data'] is Map<String, dynamic>
        ? json['data'] as Map<String, dynamic>
        : json;

    return AddressResponse(
      id: _pickString(<dynamic>[payload['id'], json['id']]) ?? '',
      userId:
          _pickString(<dynamic>[
            payload['user_id'],
            payload['userId'],
            json['user_id'],
            json['userId'],
          ]) ??
          '',
      label: _pickString(<dynamic>[payload['label'], json['label']]) ?? '',
      street:
          _pickString(<dynamic>[
            payload['calle'],
            payload['street'],
            json['calle'],
            json['street'],
          ]) ??
          '',
      colonia:
          _pickString(<dynamic>[
            payload['colonia'],
            payload['colony'],
            json['colonia'],
            json['colony'],
          ]) ??
          '',
      routeId:
          _pickString(<dynamic>[
            payload['route_id'],
            payload['routeId'],
            json['route_id'],
            json['routeId'],
          ]) ??
          '',
      verified:
          _pickBool(<dynamic>[payload['verified'], json['verified']]) ?? false,
      verifiedMethod: _pickString(<dynamic>[
        payload['verified_method'],
        payload['verifiedMethod'],
        json['verified_method'],
        json['verifiedMethod'],
      ]),
      verifiedAt: _pickString(<dynamic>[
        payload['verified_at'],
        payload['verifiedAt'],
        json['verified_at'],
        json['verifiedAt'],
      ]),
      createdAt: _pickString(<dynamic>[
        payload['created_at'],
        payload['createdAt'],
        json['created_at'],
        json['createdAt'],
      ]),
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'id': id,
      'user_id': userId,
      'label': label,
      'calle': street,
      'colonia': colonia,
      'route_id': routeId,
      'verified': verified,
      'verified_method': verifiedMethod,
      'verified_at': verifiedAt,
      'created_at': createdAt,
    };
  }

  static String? _pickString(Iterable<dynamic> candidates) {
    for (final candidate in candidates) {
      if (candidate is String && candidate.isNotEmpty) {
        return candidate;
      }
      if (candidate != null && candidate.toString().isNotEmpty) {
        return candidate.toString();
      }
    }

    return null;
  }

  static bool? _pickBool(Iterable<dynamic> candidates) {
    for (final candidate in candidates) {
      if (candidate is bool) {
        return candidate;
      }
      if (candidate is String) {
        final normalized = candidate.toLowerCase();
        if (normalized == 'true') {
          return true;
        }
        if (normalized == 'false') {
          return false;
        }
      }
    }

    return null;
  }
}
