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

part of '../../backlog.dart';

/// Configurable thresholds for backlog age markers.
///
/// The defaults match the current design spec: less than a week is fresh, less
/// than a month is aging, and anything older is stale. Keeping the thresholds in
/// a value object makes future settings/preferences easy to inject in tests or
/// user configuration.
class BacklogStalenessSettings {
  /// Creates a `BacklogStalenessSettings` instance with the values required by its invariants.
  /// Callers should pass already-normalized scheduler data because validation and defaulting belong at the model or service boundary, not in arbitrary UI code.
  const BacklogStalenessSettings({
    this.greenMaxAge = const Duration(days: 7),
    this.blueMaxAge = const Duration(days: 30),
  });

  /// Maximum age that still counts as fresh/green.
  final Duration greenMaxAge;

  /// Maximum age that still counts as aging/blue. Anything older is purple.
  final Duration blueMaxAge;

  /// Return the visual age marker for [task] relative to [now].
  ///
  /// This uses [Task.backlogFreshnessAnchorAt], not [Task.updatedAt], because
  /// ordinary edits should not make an old backlog item look fresh. Commands
  /// that deliberately re-enter Backlog reset the anchor.
  BacklogStalenessMarker markerFor({
    required Task task,
    required DateTime now,
  }) {
    final age = now.difference(task.backlogFreshnessAnchorAt);

    if (age <= greenMaxAge) {
      return BacklogStalenessMarker.green;
    }

    if (age <= blueMaxAge) {
      return BacklogStalenessMarker.blue;
    }

    return BacklogStalenessMarker.purple;
  }
}
