import '../../internal/application/EventType.dart';

/// Append-only history. This log — not the status field — is the source of
/// truth, and it is what makes excuse analysis and honest history possible.
class CommitmentEvent {
  String? id;

  String commitmentId;

  EventType event;

  DateTime? at;

  String excuseText;

  String? excuseClusterId;

  String? proofRef;

  CommitmentEvent({
    this.id,
    this.commitmentId = "",
    this.event = EventType.CREATED,
    this.at,
    this.excuseText = "",
    this.excuseClusterId,
    this.proofRef,
  });

  factory CommitmentEvent.fromJson(Map<String, dynamic> json) {
    return CommitmentEvent(
      id: json['id'],
      commitmentId: json['commitmentId'] ?? "",
      event: getEventType(json['event']),
      at: DateTime.tryParse(json['at'] ?? ""),
      excuseText: json['excuseText'] ?? "",
      excuseClusterId: json['excuseClusterId'],
      proofRef: json['proofRef'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['commitmentId'] = commitmentId;
    data['event'] = event.name;
    data['at'] = at?.toIso8601String();
    data['excuseText'] = excuseText;
    data['excuseClusterId'] = excuseClusterId;
    data['proofRef'] = proofRef;
    return data;
  }
}
