import 'dart:convert'; import '../utils/app_time.dart'; class ItServiceRequestActivityLog { ItServiceRequestActivityLog({ required this.id, required this.requestId, this.actorId, required this.actionType, this.meta, required this.createdAt, }); final String id; final String requestId; final String? actorId; final String actionType; final Map? meta; final DateTime createdAt; factory ItServiceRequestActivityLog.fromMap(Map map) { final rawId = map['id']; final rawRequestId = map['request_id']; String id = rawId == null ? '' : rawId.toString(); String requestId = rawRequestId == null ? '' : rawRequestId.toString(); final actorId = map['actor_id']?.toString(); final actionType = (map['action_type'] as String?) ?? 'unknown'; Map? meta; final rawMeta = map['meta']; if (rawMeta is Map) { meta = rawMeta; } else if (rawMeta is Map) { try { meta = rawMeta.map((k, v) => MapEntry(k.toString(), v)); } catch (_) { meta = null; } } else if (rawMeta is String && rawMeta.isNotEmpty) { try { final decoded = jsonDecode(rawMeta); if (decoded is Map) { meta = decoded; } else if (decoded is Map) { meta = decoded.map((k, v) => MapEntry(k.toString(), v)); } } catch (_) { meta = null; } } DateTime createdAt; final rawCreated = map['created_at']; if (rawCreated is String) { try { createdAt = AppTime.parse(rawCreated); } catch (_) { createdAt = AppTime.now(); } } else { createdAt = AppTime.now(); } return ItServiceRequestActivityLog( id: id, requestId: requestId, actorId: actorId, actionType: actionType, meta: meta, createdAt: createdAt, ); } }