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

part of '../../child_tasks.dart';

/// Status counts for a parent's direct children.
class ChildTaskSummary {
  /// Creates a `ChildTaskSummary` 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 ChildTaskSummary({
    required this.totalCount,
    required this.plannedCount,
    required this.activeCount,
    required this.completedCount,
    required this.missedCount,
    required this.cancelledCount,
    required this.noLongerRelevantCount,
    required this.backlogCount,
  });

  /// Build counts from [children].
  factory ChildTaskSummary.fromChildren(List<Task> children) {
    var plannedCount = 0;
    var activeCount = 0;
    var completedCount = 0;
    var missedCount = 0;
    var cancelledCount = 0;
    var noLongerRelevantCount = 0;
    var backlogCount = 0;

    for (final child in children) {
      switch (child.status) {
        case TaskStatus.planned:
          plannedCount += 1;
        case TaskStatus.active:
          activeCount += 1;
        case TaskStatus.completed:
          completedCount += 1;
        case TaskStatus.missed:
          missedCount += 1;
        case TaskStatus.cancelled:
          cancelledCount += 1;
        case TaskStatus.noLongerRelevant:
          noLongerRelevantCount += 1;
        case TaskStatus.backlog:
          backlogCount += 1;
      }
    }

    return ChildTaskSummary(
      totalCount: children.length,
      plannedCount: plannedCount,
      activeCount: activeCount,
      completedCount: completedCount,
      missedCount: missedCount,
      cancelledCount: cancelledCount,
      noLongerRelevantCount: noLongerRelevantCount,
      backlogCount: backlogCount,
    );
  }

  /// Total direct children counted.
  final int totalCount;

  /// Children in planned status.
  final int plannedCount;

  /// Children in active status.
  final int activeCount;

  /// Children in completed status.
  final int completedCount;

  /// Children in missed status.
  final int missedCount;

  /// Children in cancelled status.
  final int cancelledCount;

  /// Children marked no longer relevant.
  final int noLongerRelevantCount;

  /// Children currently in backlog.
  final int backlogCount;

  /// Whether at least one direct child exists.
  bool get hasChildren => totalCount > 0;

  /// Whether every direct child is completed.
  bool get allChildrenCompleted => hasChildren && completedCount == totalCount;
}
