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

part of '../../persistence_contract.dart';

/// Required V1 index plan.
abstract final class PersistenceIndexCatalog {
  /// Shared constant value for `all`.
  /// The constant gives this repeated scheduler or UI value one documented source of truth instead of scattering literals across call sites.
  static const all = <PersistenceIndexSpec>[
    PersistenceIndexSpec(
      name: 'tasks_owner_id_unique',
      collection: PersistenceCollections.tasks,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.taskById,
        RepositoryQueryNames.taskByOwner,
      },
    ),
    PersistenceIndexSpec(
      name: 'tasks_owner_status_backlog_age',
      collection: PersistenceCollections.tasks,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskDocumentFields.status),
        PersistenceIndexField(TaskDocumentFields.backlogEnteredAt),
        PersistenceIndexField(TaskDocumentFields.createdAt),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.taskByStatus,
        RepositoryQueryNames.taskBacklogCandidates,
      },
    ),
    PersistenceIndexSpec(
      name: 'tasks_owner_scheduled_window',
      collection: PersistenceCollections.tasks,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskDocumentFields.scheduledStart),
        PersistenceIndexField(TaskDocumentFields.scheduledEnd),
        PersistenceIndexField(DocumentFields.id),
      ],
      partialFilterFields: {
        TaskDocumentFields.scheduledStart,
        TaskDocumentFields.scheduledEnd,
      },
      supportsQueries: {
        RepositoryQueryNames.taskScheduledWindow,
        RepositoryQueryNames.taskLocalDay,
      },
    ),
    PersistenceIndexSpec(
      name: 'tasks_owner_project_status',
      collection: PersistenceCollections.tasks,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskDocumentFields.projectId),
        PersistenceIndexField(TaskDocumentFields.status),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.taskByProject,
        RepositoryQueryNames.taskBacklogCandidates,
      },
    ),
    PersistenceIndexSpec(
      name: 'tasks_owner_parent_status',
      collection: PersistenceCollections.tasks,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskDocumentFields.parentTaskId),
        PersistenceIndexField(TaskDocumentFields.status),
        PersistenceIndexField(DocumentFields.id),
      ],
      partialFilterFields: {
        TaskDocumentFields.parentTaskId,
      },
      supportsQueries: {
        RepositoryQueryNames.taskByParent,
      },
    ),
    PersistenceIndexSpec(
      name: 'projects_owner_id_unique',
      collection: PersistenceCollections.projects,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.projectById,
      },
    ),
    PersistenceIndexSpec(
      name: 'projects_owner_archived_name',
      collection: PersistenceCollections.projects,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(ProjectDocumentFields.archivedAt),
        PersistenceIndexField(ProjectDocumentFields.name),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.projectByOwner,
      },
    ),
    PersistenceIndexSpec(
      name: 'project_stats_owner_project_unique',
      collection: PersistenceCollections.projectStatistics,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(ProjectStatisticsDocumentFields.projectId),
      ],
      supportsQueries: {
        RepositoryQueryNames.projectStatisticsByProject,
      },
    ),
    PersistenceIndexSpec(
      name: 'locked_blocks_owner_id_unique',
      collection: PersistenceCollections.lockedBlocks,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.lockedBlockById,
      },
    ),
    PersistenceIndexSpec(
      name: 'locked_blocks_owner_archived_name',
      collection: PersistenceCollections.lockedBlocks,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(LockedBlockDocumentFields.archivedAt),
        PersistenceIndexField(LockedBlockDocumentFields.name),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.lockedBlockByOwner,
      },
    ),
    PersistenceIndexSpec(
      name: 'locked_overrides_owner_id_unique',
      collection: PersistenceCollections.lockedOverrides,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
    ),
    PersistenceIndexSpec(
      name: 'locked_overrides_owner_date_block',
      collection: PersistenceCollections.lockedOverrides,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(LockedBlockOverrideDocumentFields.date),
        PersistenceIndexField(LockedBlockOverrideDocumentFields.lockedBlockId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.lockedOverrideByDate,
      },
    ),
    PersistenceIndexSpec(
      name: 'task_activities_owner_id_unique',
      collection: PersistenceCollections.taskActivities,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
    ),
    PersistenceIndexSpec(
      name: 'task_activities_owner_operation',
      collection: PersistenceCollections.taskActivities,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskActivityDocumentFields.operationId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.taskActivityByOperation,
      },
    ),
    PersistenceIndexSpec(
      name: 'task_activities_owner_task_time',
      collection: PersistenceCollections.taskActivities,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskActivityDocumentFields.taskId),
        PersistenceIndexField(TaskActivityDocumentFields.occurredAt),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.taskActivityByOwner,
        RepositoryQueryNames.taskActivityByTask,
      },
    ),
    PersistenceIndexSpec(
      name: 'task_activities_owner_project_code_time',
      collection: PersistenceCollections.taskActivities,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(TaskActivityDocumentFields.projectId),
        PersistenceIndexField(TaskActivityDocumentFields.code),
        PersistenceIndexField(TaskActivityDocumentFields.occurredAt),
      ],
      supportsQueries: {
        RepositoryQueryNames.taskActivityByProject,
      },
    ),
    PersistenceIndexSpec(
      name: 'owner_settings_owner_unique',
      collection: PersistenceCollections.ownerSettings,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.ownerSettingsByOwner,
      },
    ),
    PersistenceIndexSpec(
      name: 'notice_ack_owner_notice_unique',
      collection: PersistenceCollections.noticeAcknowledgements,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(NoticeAcknowledgementDocumentFields.noticeId),
      ],
      supportsQueries: {
        RepositoryQueryNames.noticeAcknowledgementById,
      },
    ),
    PersistenceIndexSpec(
      name: 'notice_ack_owner_acknowledged',
      collection: PersistenceCollections.noticeAcknowledgements,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(
          NoticeAcknowledgementDocumentFields.acknowledgedAt,
        ),
        PersistenceIndexField(NoticeAcknowledgementDocumentFields.noticeId),
      ],
      supportsQueries: {
        RepositoryQueryNames.noticeAcknowledgementByOwner,
      },
    ),
    PersistenceIndexSpec(
      name: 'operations_owner_operation_unique',
      collection: PersistenceCollections.applicationOperations,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(
          ApplicationOperationDocumentFields.operationId,
        ),
      ],
      supportsQueries: {
        RepositoryQueryNames.operationByIdempotencyKey,
      },
    ),
    PersistenceIndexSpec(
      name: 'snapshots_owner_id_unique',
      collection: PersistenceCollections.schedulingSnapshots,
      unique: true,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(DocumentFields.id),
      ],
      supportsQueries: {
        RepositoryQueryNames.schedulingSnapshotById,
      },
    ),
    PersistenceIndexSpec(
      name: 'snapshots_owner_dates_retention',
      collection: PersistenceCollections.schedulingSnapshots,
      keys: [
        PersistenceIndexField(DocumentFields.ownerId),
        PersistenceIndexField(SchedulingSnapshotDocumentFields.sourceDate),
        PersistenceIndexField(SchedulingSnapshotDocumentFields.targetDate),
        PersistenceIndexField(
          SchedulingSnapshotDocumentFields.retentionExpiresAt,
        ),
      ],
      supportsQueries: {
        RepositoryQueryNames.schedulingSnapshotWindow,
      },
    ),
  ];

  /// Returns the derived `supportedQueries` value for this object.
  /// Keeping this as an accessor gives callers a stable read surface while the owning type controls how the value is calculated.
  static Set<String> get supportedQueries {
    return {
      for (final spec in all) ...spec.supportsQueries,
    };
  }
}
