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

/// Tests Demo Backlog Seed behavior in the FocusFlow Flutter app.
library;

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

import 'package:focus_flow_flutter/app/demo_scheduler_composition.dart';

/// Runs demo Backlog seed tests.
void main() {
  test('seeded backlog board exposes mockup-scale domain data', () async {
    final composition = DemoSchedulerComposition.seeded(
      selectedDate: CivilDate(2025, 5, 20),
    );
    addTearDown(composition.dispose);

    final board = (await composition.managementUseCases.getBacklogBoard(
      GetBacklogBoardRequest(
        context: composition.context('demo-backlog-board'),
        localDate: composition.date,
        sortKey: BacklogSortKey.age,
      ),
    )).requireValue;
    final items = board.columns.expand((column) => column.items).toList();

    expect(board.summary.totalTaskCount, 24);
    expect(board.summary.totalEstimatedMinutes, 735);
    expect(_distributionCounts(board.summary.projectDistribution), {
      'Home': 5,
      'Learning': 4,
      'Personal': 5,
      'Side Project': 2,
      'Work': 8,
    });
    expect(_distributionCounts(board.summary.priorityDistribution), {
      'High': 7,
      'Medium': 9,
      'Low': 8,
    });
    expect(items.map((item) => item.title), containsAll(_visibleSeedTitles));

    final review = items.singleWhere(
      (item) => item.title == 'Review Q3 budget',
    );
    expect(review.projectName, 'Work');
    expect(review.durationMinutes, 60);
    expect(review.priority, PriorityLevel.high);
    expect(review.reward, RewardLevel.medium);
    expect(review.difficulty, DifficultyLevel.easy);
  });

  test('seeded review detail stays within deferred metadata scope', () async {
    final composition = DemoSchedulerComposition.seeded(
      selectedDate: CivilDate(2025, 5, 20),
    );
    addTearDown(composition.dispose);

    final detail = (await composition.managementUseCases.getBacklogTaskDetail(
      GetBacklogTaskDetailRequest(
        context: composition.context('demo-review-detail'),
        localDate: composition.date,
        taskId: 'review-q3-budget',
      ),
    )).requireValue;

    expect(detail.item.title, 'Review Q3 budget');
    expect(detail.notes, isNull);
    expect(detail.tags, isEmpty);
    expect(detail.suggestedSlots, isNotEmpty);
  });
}

/// Visible task titles from the Backlog board mockup that must be present in the seed.
const _visibleSeedTitles = <String>[
  'Reply to 3 emails',
  'Update project status',
  'Schedule dentist appt',
  'Clean up downloads folder',
  'Plan tomorrow',
  'Review Q3 budget',
  'Plan next week',
  'Define weekly goals',
  'Time block schedule',
  'Organize kitchen drawers',
  'Build analytics dashboard',
  'Write marketing strategy',
  'Complete online course',
  'Module 1: Foundations',
  'Module 2: Deep Dive',
  'Module 3: Practice',
  'Learn guitar',
  'Start YouTube channel',
  'Travel to Japan',
  'Home office upgrade',
  'Read 2 books',
];

/// Top-level helper that performs the `_distributionCounts` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
Map<String, int> _distributionCounts(
  BacklogDistributionReadModel distribution,
) {
  return {for (final entry in distribution.entries) entry.label: entry.count};
}
