class UserSettings { final String userId; final int? defaultPostTtl; const UserSettings({ required this.userId, this.defaultPostTtl, }); factory UserSettings.fromJson(Map json) { return UserSettings( userId: json['user_id'] as String, defaultPostTtl: _parseIntervalHours(json['default_post_ttl']), ); } Map toJson() { return { 'user_id': userId, 'default_post_ttl': defaultPostTtl != null ? '${defaultPostTtl} hours' : null, }; } UserSettings copyWith({ int? defaultPostTtl, }) { return UserSettings( userId: userId, defaultPostTtl: defaultPostTtl ?? this.defaultPostTtl, ); } static int? _parseIntervalHours(dynamic value) { if (value == null) return null; if (value is int) return value; if (value is double) return value.round(); if (value is String) { final trimmed = value.trim(); if (trimmed.isEmpty) return null; final dayMatch = RegExp(r'(\\d+)\\s+day').firstMatch(trimmed); final hourMatch = RegExp(r'(\\d+)\\s+hour').firstMatch(trimmed); final timeMatch = RegExp(r'(\\d{1,2}):(\\d{2}):(\\d{2})').firstMatch(trimmed); var totalHours = 0; if (dayMatch != null) { totalHours += (int.tryParse(dayMatch.group(1) ?? '') ?? 0) * 24; } if (timeMatch != null) { totalHours += int.tryParse(timeMatch.group(1) ?? '') ?? 0; } else if (hourMatch != null) { totalHours += int.tryParse(hourMatch.group(1) ?? '') ?? 0; } return totalHours == 0 ? null : totalHours; } return null; } }