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

/// Tests completion date presentation models.
library;

import 'package:flutter_test/flutter_test.dart';
import 'package:scheduler_core/scheduler_core.dart';

import 'package:focus_flow_flutter/models/today_screen_models.dart';

/// Runs completion date presentation tests.
void main() {
  test('same-day completion keeps compact time-only text', () {
    final card = TimelineCardModel.fromItem(
      _completedItem(
        scheduledStart: DateTime.utc(2026, 1, 3, 9),
        scheduledEnd: DateTime.utc(2026, 1, 3, 9, 30),
        completedAt: DateTime.utc(2026, 1, 3, 9, 25),
      ),
      now: DateTime.utc(2026, 1, 3, 10),
    );

    expect(card.completionDateContextRequired, isFalse);
    expect(card.completedTimeText, '9:25 AM');
    expect(card.subtitle, 'Completed at: 9:25 AM');
  });

  test('completedAt on a different date includes date context', () {
    final card = TimelineCardModel.fromItem(
      _completedItem(
        scheduledStart: DateTime.utc(2026, 1, 3, 9),
        scheduledEnd: DateTime.utc(2026, 1, 3, 9, 30),
        completedAt: DateTime.utc(2026, 1, 4, 8, 5),
      ),
      now: DateTime.utc(2026, 1, 4, 9),
    );

    expect(card.completionDateContextRequired, isTrue);
    expect(card.completedTimeText, 'Jan 4, 8:05 AM');
    expect(card.subtitle, 'Completed at: Jan 4, 8:05 AM');
  });

  test('actual interval on a different date includes date context', () {
    final card = TimelineCardModel.fromItem(
      _completedItem(
        scheduledStart: DateTime.utc(2026, 1, 3, 23, 30),
        scheduledEnd: DateTime.utc(2026, 1, 4),
        completedAt: DateTime.utc(2026, 1, 3, 23, 45),
        actualStart: DateTime.utc(2026, 1, 4, 0, 10),
        actualEnd: DateTime.utc(2026, 1, 4, 0, 20),
      ),
      now: DateTime.utc(2026, 1, 4, 1),
    );

    expect(card.completionDateContextRequired, isTrue);
    expect(card.completedTimeText, 'Jan 3, 11:45 PM');
    expect(card.subtitle, 'Completed at: Jan 3, 11:45 PM');
  });
}

/// Builds a completed timeline item for presentation tests.
TodayTimelineItem _completedItem({
  required DateTime scheduledStart,
  required DateTime scheduledEnd,
  DateTime? completedAt,
  DateTime? actualStart,
  DateTime? actualEnd,
}) {
  return TodayTimelineItem(
    item: TimelineItem(
      id: 'completed-task',
      displayTitle: 'Completed task',
      taskType: TaskType.flexible,
      projectColorToken: 'project-home',
      backgroundToken: TimelineBackgroundToken.flexible,
      rewardIconToken: TimelineRewardIconToken.medium,
      difficultyIconToken: TimelineDifficultyIconToken.medium,
      showsExplicitTime: true,
      quickActions: const [],
      category: TimelineItemCategory.taskCard,
      start: scheduledStart,
      end: scheduledEnd,
      actualStart: actualStart,
      actualEnd: actualEnd,
      completedAt: completedAt,
      durationMinutes: scheduledEnd.difference(scheduledStart).inMinutes,
    ),
    source: TodayTimelineItemSource.task,
    taskStatus: TaskStatus.completed,
    taskId: 'completed-task',
  );
}
