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

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

/// Document mapping for [LockedBlock].
abstract final class LockedBlockDocumentMapping {
  /// 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(
    LockedBlock block, {
    required String ownerId,
    int revision = 1,
  }) {
    return {
      ..._commonFields(
        id: block.id,
        ownerId: ownerId,
        revision: revision,
        createdAt: block.createdAt,
        updatedAt: block.updatedAt,
      ),
      LockedBlockDocumentFields.name: block.name,
      LockedBlockDocumentFields.startTime:
          PersistenceWallTimeConvention.toStoredString(block.startTime),
      LockedBlockDocumentFields.endTime:
          PersistenceWallTimeConvention.toStoredString(block.endTime),
      LockedBlockDocumentFields.date: _civilDateToStored(block.date),
      LockedBlockDocumentFields.recurrence: block.recurrence == null
          ? null
          : LockedBlockRecurrenceDocumentMapping.toDocument(
              block.recurrence!,
            ),
      LockedBlockDocumentFields.hiddenByDefault: block.hiddenByDefault,
      LockedBlockDocumentFields.projectId: block.projectId,
      LockedBlockDocumentFields.archivedAt: _dateTimeToStored(block.archivedAt),
    };
  }

  /// 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 LockedBlock fromDocument(Map<String, Object?> document) {
    return _mapInvalidValues(() {
      _readCommon(document);
      final recurrenceDocument = _requiredNullableMap(
        document,
        LockedBlockDocumentFields.recurrence,
      );
      return LockedBlock(
        id: _requiredString(document, LockedBlockDocumentFields.id),
        name: _requiredString(document, LockedBlockDocumentFields.name),
        startTime: _requiredWallTime(
          document,
          LockedBlockDocumentFields.startTime,
        ),
        endTime: _requiredWallTime(
          document,
          LockedBlockDocumentFields.endTime,
        ),
        date: _requiredNullableCivilDate(
          document,
          LockedBlockDocumentFields.date,
        ),
        recurrence: recurrenceDocument == null
            ? null
            : LockedBlockRecurrenceDocumentMapping.fromDocument(
                recurrenceDocument,
              ),
        hiddenByDefault: _requiredBool(
          document,
          LockedBlockDocumentFields.hiddenByDefault,
        ),
        projectId: _requiredNullableString(
          document,
          LockedBlockDocumentFields.projectId,
        ),
        createdAt:
            _requiredDateTime(document, LockedBlockDocumentFields.createdAt),
        updatedAt:
            _requiredDateTime(document, LockedBlockDocumentFields.updatedAt),
        archivedAt: _requiredNullableDateTime(
          document,
          LockedBlockDocumentFields.archivedAt,
        ),
      );
    });
  }
}
