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

part of '../../application_commands.dart';

/// Structured result returned by V1 application commands.
class ApplicationCommandResult {
  /// Creates a `ApplicationCommandResult` 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.
  ApplicationCommandResult({
    required this.commandCode,
    required String operationId,
    required List<Task> changedTasks,
    required this.readHint,
    List<TaskActivity> activities = const <TaskActivity>[],
    List<ProjectStatistics> projectStatistics = const <ProjectStatistics>[],
    List<SchedulingNotice> notices = const <SchedulingNotice>[],
    List<SchedulingChange> changes = const <SchedulingChange>[],
    List<SchedulingOverlap> overlaps = const <SchedulingOverlap>[],
    List<ProtectedFreeSlotConflict> protectedFreeSlotConflicts =
        const <ProtectedFreeSlotConflict>[],
    List<String> childTaskIds = const <String>[],
    this.schedulingResult,
  })  : operationId = _requiredTrimmed(operationId, 'operationId'),
        changedTasks = List<Task>.unmodifiable(changedTasks),
        activities = List<TaskActivity>.unmodifiable(activities),
        projectStatistics = List<ProjectStatistics>.unmodifiable(
          projectStatistics,
        ),
        notices = List<SchedulingNotice>.unmodifiable(notices),
        changes = List<SchedulingChange>.unmodifiable(changes),
        overlaps = List<SchedulingOverlap>.unmodifiable(overlaps),
        protectedFreeSlotConflicts =
            List<ProtectedFreeSlotConflict>.unmodifiable(
          protectedFreeSlotConflicts,
        ),
        childTaskIds = List<String>.unmodifiable(childTaskIds);

  /// Command that produced this result.
  final ApplicationCommandCode commandCode;

  /// Exactly-once operation id.
  final String operationId;

  /// Tasks inserted or changed by the command.
  final List<Task> changedTasks;

  /// New internal activities persisted with the command.
  final List<TaskActivity> activities;

  /// Project aggregates updated by completion activities.
  final List<ProjectStatistics> projectStatistics;

  /// Scheduling notices returned by domain services.
  final List<SchedulingNotice> notices;

  /// Scheduling movement records returned by domain services.
  final List<SchedulingChange> changes;

  /// Scheduling overlaps returned by domain services.
  final List<SchedulingOverlap> overlaps;

  /// Protected rest conflicts caused by an explicit required placement.
  final List<ProtectedFreeSlotConflict> protectedFreeSlotConflicts;

  /// Child task ids created or affected by child-task commands.
  final List<String> childTaskIds;

  /// Full scheduling result when the command invoked the scheduler.
  final SchedulingResult? schedulingResult;

  /// Read-model refresh hint for UI/state management.
  final ApplicationCommandReadHint readHint;
}
