// SPDX-FileCopyrightText: 2026 FocusFlow contributors
// SPDX-License-Identifier: AGPL-3.0-only

part of '../../document_mapping.dart';

/// Document mapping for [ApplicationOperationRecord].
abstract final class ApplicationOperationDocumentMapping {
  /// Converts scheduler data for `toDocument` without changing the source object.
  /// Conversion helpers keep persistence and export shapes explicit so domain models do not depend on storage-specific details.
  static Map<String, Object?> toDocument(
    ApplicationOperationRecord record, {
    int revision = 1,
  }) {
    return {
      ..._commonFields(
        id: record.operationId,
        ownerId: record.ownerId,
        revision: revision,
        createdAt: record.committedAt,
        updatedAt: record.committedAt,
      ),
      ApplicationOperationDocumentFields.operationId: record.operationId,
      ApplicationOperationDocumentFields.operationName: record.operationName,
      ApplicationOperationDocumentFields.committedAt:
          PersistenceDateTimeConvention.toStoredString(record.committedAt),
    };
  }

  /// Converts scheduler data for `fromDocument` without changing the source object.
  /// Conversion helpers keep persistence and export shapes explicit so domain models do not depend on storage-specific details.
  static ApplicationOperationRecord fromDocument(
    Map<String, Object?> document,
  ) {
    return _mapInvalidValues(() {
      _readCommon(document);
      return ApplicationOperationRecord(
        operationId: _requiredString(
          document,
          ApplicationOperationDocumentFields.operationId,
        ),
        ownerId: _requiredString(
          document,
          ApplicationOperationDocumentFields.ownerId,
        ),
        operationName: _requiredString(
          document,
          ApplicationOperationDocumentFields.operationName,
        ),
        committedAt: _requiredDateTime(
          document,
          ApplicationOperationDocumentFields.committedAt,
        ),
      );
    });
  }
}
