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

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

/// Row-style input for creating one child task.
///
/// Priority is nullable on purpose. A null priority means the user did not set
/// one, so callers should preserve row insertion order instead of treating the
/// child as medium priority.
class ChildTaskEntry {
  /// Creates a `ChildTaskEntry` 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 ChildTaskEntry({
    required this.id,
    required this.title,
    required this.createdAt,
    this.priority,
    this.reward = RewardLevel.notSet,
    this.difficulty = DifficultyLevel.notSet,
    this.durationMinutes,
    this.projectId,
    this.updatedAt,
  });

  /// Caller-generated child id.
  final String id;

  /// User-entered child title.
  final String title;

  /// Creation timestamp supplied by the caller.
  final DateTime createdAt;

  /// Optional explicit child priority.
  final PriorityLevel? priority;

  /// Optional explicit child reward. Missing reward stays `notSet`.
  final RewardLevel reward;

  /// Optional explicit child difficulty.
  final DifficultyLevel difficulty;

  /// Optional child duration estimate.
  final int? durationMinutes;

  /// Optional project override. Null means inherit the parent project.
  final String? projectId;

  /// Optional update timestamp.
  final DateTime? updatedAt;

  /// Convert this entry into a child [Task] owned by [parent].
  Task toTask({
    required Task parent,
  }) {
    final trimmedTitle = title.trim();
    if (trimmedTitle.isEmpty) {
      throw ArgumentError.value(title, 'title', 'Title is required.');
    }

    return Task(
      id: id,
      title: trimmedTitle,
      projectId: projectId ?? parent.projectId,
      type: TaskType.flexible,
      status: TaskStatus.backlog,
      priority: priority,
      reward: reward,
      difficulty: difficulty,
      durationMinutes: durationMinutes,
      parentTaskId: parent.id,
      createdAt: createdAt,
      updatedAt: updatedAt ?? createdAt,
    );
  }
}
