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

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

/// Document mapping for [ProjectStatistics].
abstract final class ProjectStatisticsDocumentMapping {
  /// 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(
    ProjectStatistics statistics, {
    required String ownerId,
    required DateTime createdAt,
    required DateTime updatedAt,
    int revision = 1,
    Iterable<String> appliedActivityIds = const <String>[],
  }) {
    return {
      ..._commonFields(
        id: statistics.projectId,
        ownerId: ownerId,
        revision: revision,
        createdAt: createdAt,
        updatedAt: updatedAt,
      ),
      ProjectStatisticsDocumentFields.projectId: statistics.projectId,
      ProjectStatisticsDocumentFields.completedTaskCount:
          statistics.completedTaskCount,
      ProjectStatisticsDocumentFields.durationMinuteCounts:
          _intKeyCountMapToDocument(statistics.durationMinuteCounts),
      ProjectStatisticsDocumentFields.completionTimeBucketCounts:
          _enumCountMapToDocument(
        statistics.completionTimeBucketCounts,
        PersistenceEnumMapping.encodeProjectCompletionTimeBucket,
      ),
      ProjectStatisticsDocumentFields.totalPushesBeforeCompletion:
          statistics.totalPushesBeforeCompletion,
      ProjectStatisticsDocumentFields.completedAfterPushCount:
          statistics.completedAfterPushCount,
      ProjectStatisticsDocumentFields.rewardCounts: _enumCountMapToDocument(
        statistics.rewardCounts,
        PersistenceEnumMapping.encodeReward,
      ),
      ProjectStatisticsDocumentFields.difficultyCounts: _enumCountMapToDocument(
        statistics.difficultyCounts,
        PersistenceEnumMapping.encodeDifficulty,
      ),
      ProjectStatisticsDocumentFields.reminderProfileCounts:
          _enumCountMapToDocument(
        statistics.reminderProfileCounts,
        PersistenceEnumMapping.encodeReminderProfile,
      ),
      ProjectStatisticsDocumentFields.appliedActivityIds:
          appliedActivityIds.toList(growable: false),
    };
  }

  /// 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 ProjectStatistics fromDocument(Map<String, Object?> document) {
    return _mapInvalidValues(() {
      _readCommon(document);
      return ProjectStatistics(
        projectId: _requiredString(
          document,
          ProjectStatisticsDocumentFields.projectId,
        ),
        completedTaskCount: _requiredInt(
          document,
          ProjectStatisticsDocumentFields.completedTaskCount,
        ),
        durationMinuteCounts: _intKeyCountMapFromDocument(
          document,
          ProjectStatisticsDocumentFields.durationMinuteCounts,
        ),
        completionTimeBucketCounts: _enumCountMapFromDocument(
          document,
          ProjectStatisticsDocumentFields.completionTimeBucketCounts,
          PersistenceEnumMapping.decodeProjectCompletionTimeBucket,
        ),
        totalPushesBeforeCompletion: _requiredInt(
          document,
          ProjectStatisticsDocumentFields.totalPushesBeforeCompletion,
        ),
        completedAfterPushCount: _requiredInt(
          document,
          ProjectStatisticsDocumentFields.completedAfterPushCount,
        ),
        rewardCounts: _enumCountMapFromDocument(
          document,
          ProjectStatisticsDocumentFields.rewardCounts,
          PersistenceEnumMapping.decodeReward,
        ),
        difficultyCounts: _enumCountMapFromDocument(
          document,
          ProjectStatisticsDocumentFields.difficultyCounts,
          PersistenceEnumMapping.decodeDifficulty,
        ),
        reminderProfileCounts: _enumCountMapFromDocument(
          document,
          ProjectStatisticsDocumentFields.reminderProfileCounts,
          (value) =>
              PersistenceEnumMapping.decodeNullableReminderProfile(value)!,
        ),
      ).._validateProjectStatisticsDocumentMetadata(document);
    });
  }
}
