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

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

/// Document mapping for [OwnerSettings].
abstract final class OwnerSettingsDocumentMapping {
  /// 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(
    OwnerSettings settings, {
    required DateTime createdAt,
    required DateTime updatedAt,
    int revision = 1,
  }) {
    return {
      ..._commonFields(
        id: settings.ownerId,
        ownerId: settings.ownerId,
        revision: revision,
        createdAt: createdAt,
        updatedAt: updatedAt,
      ),
      OwnerSettingsDocumentFields.timeZoneId: settings.timeZoneId,
      OwnerSettingsDocumentFields.dayStart:
          PersistenceWallTimeConvention.toStoredString(settings.dayStart),
      OwnerSettingsDocumentFields.dayEnd:
          PersistenceWallTimeConvention.toStoredString(settings.dayEnd),
      OwnerSettingsDocumentFields.compactModeEnabled:
          settings.compactModeEnabled,
      OwnerSettingsDocumentFields.backlogStaleness:
          BacklogStalenessDocumentMapping.toDocument(
        settings.backlogStalenessSettings,
      ),
    };
  }

  /// 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 OwnerSettings fromDocument(Map<String, Object?> document) {
    return _mapInvalidValues(() {
      _readCommon(document);
      return OwnerSettings(
        ownerId: _requiredString(document, OwnerSettingsDocumentFields.ownerId),
        timeZoneId: _requiredString(
          document,
          OwnerSettingsDocumentFields.timeZoneId,
        ),
        dayStart: _requiredWallTime(
          document,
          OwnerSettingsDocumentFields.dayStart,
        ),
        dayEnd: _requiredWallTime(
          document,
          OwnerSettingsDocumentFields.dayEnd,
        ),
        compactModeEnabled: _requiredBool(
          document,
          OwnerSettingsDocumentFields.compactModeEnabled,
        ),
        backlogStalenessSettings: BacklogStalenessDocumentMapping.fromDocument(
          _requiredMap(document, OwnerSettingsDocumentFields.backlogStaleness),
        ),
      );
    });
  }
}
