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

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

/// Document mapping for [LockedBlockOverride].
abstract final class LockedBlockOverrideDocumentMapping {
  /// 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(
    LockedBlockOverride override, {
    required String ownerId,
    int revision = 1,
  }) {
    return {
      ..._commonFields(
        id: override.id,
        ownerId: ownerId,
        revision: revision,
        createdAt: override.createdAt,
        updatedAt: override.updatedAt,
      ),
      LockedBlockOverrideDocumentFields.lockedBlockId: override.lockedBlockId,
      LockedBlockOverrideDocumentFields.date:
          PersistenceCivilDateConvention.toStoredString(override.date),
      LockedBlockOverrideDocumentFields.type:
          PersistenceEnumMapping.encodeLockedBlockOverrideType(override.type),
      LockedBlockOverrideDocumentFields.name: override.name,
      LockedBlockOverrideDocumentFields.startTime:
          _wallTimeToStored(override.startTime),
      LockedBlockOverrideDocumentFields.endTime:
          _wallTimeToStored(override.endTime),
      LockedBlockOverrideDocumentFields.hiddenByDefault:
          override.hiddenByDefault,
      LockedBlockOverrideDocumentFields.projectId: override.projectId,
    };
  }

  /// 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 LockedBlockOverride fromDocument(Map<String, Object?> document) {
    return _mapInvalidValues(() {
      _readCommon(document);
      return LockedBlockOverride(
        id: _requiredString(document, LockedBlockOverrideDocumentFields.id),
        date: _requiredCivilDate(
          document,
          LockedBlockOverrideDocumentFields.date,
        ),
        type: PersistenceEnumMapping.decodeLockedBlockOverrideType(
          _requiredString(document, LockedBlockOverrideDocumentFields.type),
        ),
        createdAt: _requiredDateTime(
          document,
          LockedBlockOverrideDocumentFields.createdAt,
        ),
        updatedAt: _requiredDateTime(
          document,
          LockedBlockOverrideDocumentFields.updatedAt,
        ),
        lockedBlockId: _requiredNullableString(
          document,
          LockedBlockOverrideDocumentFields.lockedBlockId,
        ),
        name: _requiredNullableString(
          document,
          LockedBlockOverrideDocumentFields.name,
        ),
        startTime: _requiredNullableWallTime(
          document,
          LockedBlockOverrideDocumentFields.startTime,
        ),
        endTime: _requiredNullableWallTime(
          document,
          LockedBlockOverrideDocumentFields.endTime,
        ),
        hiddenByDefault: _requiredBool(
          document,
          LockedBlockOverrideDocumentFields.hiddenByDefault,
        ),
        projectId: _requiredNullableString(
          document,
          LockedBlockOverrideDocumentFields.projectId,
        ),
      );
    });
  }
}
