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

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

/// Document mapping for [ProjectProfile].
abstract final class ProjectDocumentMapping {
  /// 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(
    ProjectProfile project, {
    required String ownerId,
    required DateTime createdAt,
    required DateTime updatedAt,
    int revision = 1,
  }) {
    return {
      ..._commonFields(
        id: project.id,
        ownerId: ownerId,
        revision: revision,
        createdAt: createdAt,
        updatedAt: updatedAt,
      ),
      ProjectDocumentFields.name: project.name,
      ProjectDocumentFields.colorKey: project.colorKey,
      ProjectDocumentFields.defaultPriority:
          PersistenceEnumMapping.encodePriority(project.defaultPriority),
      ProjectDocumentFields.defaultReward:
          PersistenceEnumMapping.encodeReward(project.defaultReward),
      ProjectDocumentFields.defaultDifficulty:
          PersistenceEnumMapping.encodeDifficulty(project.defaultDifficulty),
      ProjectDocumentFields.defaultReminderProfile:
          PersistenceEnumMapping.encodeReminderProfile(
        project.defaultReminderProfile,
      ),
      ProjectDocumentFields.defaultDurationMinutes:
          project.defaultDurationMinutes,
      ProjectDocumentFields.archivedAt: _dateTimeToStored(project.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 ProjectProfile fromDocument(Map<String, Object?> document) {
    return _mapInvalidValues(() {
      _readCommon(document);
      return ProjectProfile(
        id: _requiredString(document, ProjectDocumentFields.id),
        name: _requiredString(document, ProjectDocumentFields.name),
        colorKey: _requiredString(document, ProjectDocumentFields.colorKey),
        defaultPriority: PersistenceEnumMapping.decodeNullablePriority(
          _requiredString(document, ProjectDocumentFields.defaultPriority),
        )!,
        defaultReward: PersistenceEnumMapping.decodeReward(
          _requiredString(document, ProjectDocumentFields.defaultReward),
        ),
        defaultDifficulty: PersistenceEnumMapping.decodeDifficulty(
          _requiredString(document, ProjectDocumentFields.defaultDifficulty),
        ),
        defaultReminderProfile:
            PersistenceEnumMapping.decodeNullableReminderProfile(
          _requiredString(
            document,
            ProjectDocumentFields.defaultReminderProfile,
          ),
        )!,
        defaultDurationMinutes: _requiredNullableInt(
          document,
          ProjectDocumentFields.defaultDurationMinutes,
        ),
        archivedAt: _requiredNullableDateTime(
          document,
          ProjectDocumentFields.archivedAt,
        ),
      );
    });
  }
}
