import 'package:ac_web/Api/Dtos/AgentTaskDto.dart'; import 'package:ac_web/Api/Dtos/EAgentStatus.dart'; import 'package:ac_web/Api/Dtos/EAgentType.dart'; import 'package:ac_web/Api/JsonReader.dart'; /// Dart counterpart of the backend `AgentData` returned by `GET /agents`. class AgentDto { const AgentDto({ required this.name, required this.type, required this.lastSeen, required this.tasks, required this.status, }); final String name; final EAgentType type; final DateTime lastSeen; final List tasks; final EAgentStatus status; bool get hasTasks => tasks.isNotEmpty; factory AgentDto.fromJson(Map json) { return AgentDto( name: JsonReader.requireString(json, 'Name'), type: JsonReader.enumValue( json, 'Type', EAgentType.values, (value) => value.name, EAgentType.unknown, ), lastSeen: JsonReader.requireDateTime(json, 'LastSeen'), tasks: JsonReader.list(json, 'Tasks', AgentTaskDto.fromJson), status: JsonReader.enumValue( json, 'Status', EAgentStatus.values, (value) => value.name, EAgentStatus.unknown, ), ); } Map toJson() { return { 'Name': name, 'Type': type.name, 'LastSeen': lastSeen.toIso8601String(), 'Tasks': tasks.map((task) => task.toJson()).toList(growable: false), 'Status': status.name, }; } @override String toString() => 'AgentDto(name: $name, type: ${type.name}, status: ${status.name})'; }