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

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

/// Document mapping for [SchedulingNotice].
abstract final class SchedulingNoticeDocumentMapping {
  /// 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(SchedulingNotice notice) {
    return {
      SchedulingNoticeDocumentFields.message: notice.message,
      SchedulingNoticeDocumentFields.type:
          PersistenceEnumMapping.encodeSchedulingNoticeType(notice.type),
      SchedulingNoticeDocumentFields.taskId: notice.taskId,
      SchedulingNoticeDocumentFields.issueCode: notice.issueCode == null
          ? null
          : PersistenceEnumMapping.encodeSchedulingIssueCode(notice.issueCode!),
      SchedulingNoticeDocumentFields.movementCode: notice.movementCode == null
          ? null
          : PersistenceEnumMapping.encodeSchedulingMovementCode(
              notice.movementCode!,
            ),
      SchedulingNoticeDocumentFields.conflictCode: notice.conflictCode == null
          ? null
          : PersistenceEnumMapping.encodeSchedulingConflictCode(
              notice.conflictCode!,
            ),
      SchedulingNoticeDocumentFields.parameters:
          _plainDocument(notice.parameters),
    };
  }

  /// 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 SchedulingNotice fromDocument(Map<String, Object?> document) {
    return _mapInvalidValues(() {
      return SchedulingNotice(
        _requiredString(document, SchedulingNoticeDocumentFields.message),
        type: PersistenceEnumMapping.decodeSchedulingNoticeType(
          _requiredString(document, SchedulingNoticeDocumentFields.type),
        ),
        taskId: _requiredNullableString(
          document,
          SchedulingNoticeDocumentFields.taskId,
        ),
        issueCode: PersistenceEnumMapping.decodeNullableSchedulingIssueCode(
          _requiredNullableString(
            document,
            SchedulingNoticeDocumentFields.issueCode,
          ),
        ),
        movementCode:
            PersistenceEnumMapping.decodeNullableSchedulingMovementCode(
          _requiredNullableString(
            document,
            SchedulingNoticeDocumentFields.movementCode,
          ),
        ),
        conflictCode:
            PersistenceEnumMapping.decodeNullableSchedulingConflictCode(
          _requiredNullableString(
            document,
            SchedulingNoticeDocumentFields.conflictCode,
          ),
        ),
        parameters: _requiredMap(
          document,
          SchedulingNoticeDocumentFields.parameters,
        ),
      );
    });
  }
}
