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

part of '../today_screen_models.dart';

/// Top-level helper that performs the `_visualKindFor` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
TaskVisualKind _visualKindFor(TodayTimelineItem item) {
  if (item.taskStatus == TaskStatus.completed ||
      (item.taskType == TaskType.surprise &&
          item.taskStatus == TaskStatus.completed)) {
    return TaskVisualKind.completedSurprise;
  }
  return switch (item.taskType) {
    TaskType.flexible => TaskVisualKind.flexible,
    TaskType.critical => TaskVisualKind.required,
    TaskType.inflexible => TaskVisualKind.appointment,
    TaskType.freeSlot => TaskVisualKind.freeSlot,
    TaskType.surprise => TaskVisualKind.completedSurprise,
    TaskType.locked => TaskVisualKind.completedSurprise,
  };
}

/// Top-level helper that performs the `_typeLabelFor` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String _typeLabelFor(TaskType taskType, TaskVisualKind visualKind) {
  if (visualKind == TaskVisualKind.completedSurprise) {
    return 'Completed';
  }
  return switch (taskType) {
    TaskType.flexible => 'Flexible',
    TaskType.critical => 'Required',
    TaskType.inflexible => 'Required',
    TaskType.freeSlot => 'Free Slot',
    TaskType.surprise => 'Surprise task',
    TaskType.locked => 'Locked',
  };
}

/// Top-level helper that performs the `_subtitleFor` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String _subtitleFor(
  TodayTimelineItem item,
  TaskVisualKind visualKind,
  String timeText, {
  required bool completionDateContextRequired,
  required Duration timeZoneOffset,
}) {
  if (visualKind == TaskVisualKind.freeSlot) {
    return 'Intentional rest';
  }
  if (item.taskStatus == TaskStatus.completed ||
      visualKind == TaskVisualKind.completedSurprise) {
    final completedText = _completionDisplayText(
      item,
      includeDate: completionDateContextRequired,
      timeZoneOffset: timeZoneOffset,
    );
    if (completedText == null || completedText.isEmpty) {
      return 'Completed';
    }
    return 'Completed at: $completedText';
  }
  if (item.taskType == TaskType.flexible && item.item.durationMinutes != null) {
    return '${item.item.durationMinutes} min';
  }
  return timeText;
}

/// Top-level helper that performs the `_completionDisplayText` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String? _completionDisplayText(
  TodayTimelineItem item, {
  required bool includeDate,
  required Duration timeZoneOffset,
}) {
  final completionInstant = _completionDisplayInstant(item);
  if (completionInstant == null) {
    return null;
  }
  if (includeDate) {
    return _shortDateTimeLabel(
      completionInstant,
      timeZoneOffset: timeZoneOffset,
    );
  }
  return _formatTime(completionInstant, timeZoneOffset: timeZoneOffset);
}

/// Top-level helper that performs the `_completionDisplayInstant` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
DateTime? _completionDisplayInstant(TodayTimelineItem item) {
  return item.item.completedAt ?? item.item.actualEnd ?? item.item.end;
}

/// Top-level helper that performs the `_completionDateContextRequired` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
bool _completionDateContextRequired(
  TodayTimelineItem item, {
  required Duration timeZoneOffset,
}) {
  final scheduledDate = _civilDateForDateTime(
    item.item.start ?? item.start,
    timeZoneOffset: timeZoneOffset,
  );
  if (scheduledDate == null) {
    return false;
  }
  final completedAtDate = _civilDateForDateTime(
    item.item.completedAt,
    timeZoneOffset: timeZoneOffset,
  );
  final actualStartDate = _civilDateForDateTime(
    item.item.actualStart,
    timeZoneOffset: timeZoneOffset,
  );
  final actualEndDate = _civilDateForDateTime(
    item.item.actualEnd,
    timeZoneOffset: timeZoneOffset,
  );
  return completedAtDate != null && completedAtDate != scheduledDate ||
      actualStartDate != null && actualStartDate != scheduledDate ||
      actualEndDate != null && actualEndDate != scheduledDate;
}

/// Top-level helper that performs the `_showPastTaskPushButton` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
bool _showPastTaskPushButton(TodayTimelineItem item, {required DateTime now}) {
  if (item.source != TodayTimelineItemSource.task) {
    return false;
  }
  if (item.taskType != TaskType.flexible) {
    return false;
  }
  if (item.taskStatus != TaskStatus.planned) {
    return false;
  }
  final end = item.end;
  return end != null && end.isBefore(now);
}
