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

/// Tests Backlog summary panel rendering and collapse behavior.
library;

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

import 'package:focus_flow_flutter/theme/focus_flow_theme.dart';
import 'package:focus_flow_flutter/widgets/backlog/backlog_summary_panel.dart';

/// Runs Backlog summary panel widget tests.
void main() {
  testWidgets('renders summary sections and toggles collapse state', (
    tester,
  ) async {
    await _pumpSummaryPanel(tester, _summary());

    expect(find.text('Backlog summary'), findsOneWidget);
    expect(find.text('24'), findsOneWidget);
    expect(find.text('12h 15m'), findsOneWidget);
    expect(find.text('Total tasks'), findsOneWidget);
    expect(find.text('Total estimated time'), findsOneWidget);
    expect(find.text('By priority'), findsOneWidget);
    expect(find.text('High'), findsOneWidget);
    expect(find.text('Medium'), findsOneWidget);
    expect(find.text('Low'), findsOneWidget);
    expect(find.text('By project'), findsOneWidget);
    expect(find.text('Home'), findsOneWidget);
    expect(find.text('Work'), findsOneWidget);
    expect(find.text('Duration distribution'), findsOneWidget);
    expect(find.text('0-30 min'), findsOneWidget);
    expect(find.text('7 (29%)'), findsOneWidget);
    expect(find.text('30-60 min'), findsOneWidget);
    expect(find.text('9 (38%)'), findsOneWidget);
    expect(find.text('60-120 min'), findsOneWidget);
    expect(find.text('6 (25%)'), findsOneWidget);
    expect(find.text('120+ min'), findsOneWidget);
    expect(find.text('2 (8%)'), findsOneWidget);
    expect(find.text('Planning tip'), findsOneWidget);
    expect(find.text(_planningTip), findsOneWidget);

    await tester.tap(
      find.byKey(const ValueKey('backlog-summary-collapse-button')),
    );
    await tester.pumpAndSettle();

    expect(find.text('Backlog summary'), findsOneWidget);
    expect(find.text('By priority'), findsNothing);
    expect(find.text('Duration distribution'), findsNothing);
    expect(find.text(_planningTip), findsNothing);

    await tester.tap(
      find.byKey(const ValueKey('backlog-summary-collapse-button')),
    );
    await tester.pumpAndSettle();

    expect(find.text('By priority'), findsOneWidget);
    expect(find.text('Duration distribution'), findsOneWidget);
    expect(find.text(_planningTip), findsOneWidget);
  });
}

/// Planning guidance text used by the panel fixture.
const _planningTip =
    'Start with the small high-priority work, then reserve a block for the longer items.';

/// Pumps [summary] in a fixed-width dark FocusFlow test harness.
Future<void> _pumpSummaryPanel(
  WidgetTester tester,
  BacklogBoardSummaryReadModel summary,
) async {
  await tester.binding.setSurfaceSize(const Size(420, 760));
  addTearDown(() => tester.binding.setSurfaceSize(null));
  await tester.pumpWidget(
    MaterialApp(
      theme: FocusFlowTheme.dark(),
      home: Scaffold(
        body: Center(
          child: SizedBox(
            width: 320,
            height: 720,
            child: BacklogSummaryPanel(summary: summary),
          ),
        ),
      ),
    ),
  );
  await tester.pumpAndSettle();
}

/// Builds a representative summary read model for panel tests.
BacklogBoardSummaryReadModel _summary() {
  return BacklogBoardSummaryReadModel(
    totalTaskCount: 24,
    totalEstimatedMinutes: 735,
    priorityDistribution: BacklogDistributionReadModel(
      title: 'Priority',
      entries: const [
        BacklogDistributionEntryReadModel(
          id: 'high',
          label: 'High',
          count: 7,
          percentage: 0.29,
          colorToken: 'priority-high',
        ),
        BacklogDistributionEntryReadModel(
          id: 'medium',
          label: 'Medium',
          count: 9,
          percentage: 0.38,
          colorToken: 'priority-medium',
        ),
        BacklogDistributionEntryReadModel(
          id: 'low',
          label: 'Low',
          count: 8,
          percentage: 0.33,
          colorToken: 'priority-low',
        ),
      ],
    ),
    projectDistribution: BacklogDistributionReadModel(
      title: 'Project',
      entries: const [
        BacklogDistributionEntryReadModel(
          id: 'home',
          label: 'Home',
          count: 10,
          percentage: 0.42,
          colorToken: 'project-home',
        ),
        BacklogDistributionEntryReadModel(
          id: 'work',
          label: 'Work',
          count: 8,
          percentage: 0.33,
          colorToken: 'project-work',
        ),
        BacklogDistributionEntryReadModel(
          id: 'personal',
          label: 'Personal',
          count: 6,
          percentage: 0.25,
          colorToken: 'project-personal',
        ),
      ],
    ),
    durationDistribution: BacklogDistributionReadModel(
      title: 'Duration',
      entries: const [
        BacklogDistributionEntryReadModel(
          id: '0-30',
          label: '0-30 min',
          count: 7,
          percentage: 0.29,
        ),
        BacklogDistributionEntryReadModel(
          id: '30-60',
          label: '30-60 min',
          count: 9,
          percentage: 0.38,
        ),
        BacklogDistributionEntryReadModel(
          id: '60-120',
          label: '60-120 min',
          count: 6,
          percentage: 0.25,
        ),
        BacklogDistributionEntryReadModel(
          id: '120+',
          label: '120+ min',
          count: 2,
          percentage: 0.08,
        ),
      ],
    ),
    planningTip: _planningTip,
  );
}
