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

/// Tests app-level date navigation 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/app/demo_scheduler_composition.dart';
import 'package:focus_flow_flutter/app/focus_flow_app.dart';

/// Runs app-level date navigation tests.
void main() {
  testWidgets('previous and next arrows reload selected dates', (tester) async {
    await _pumpPlanApp(tester);

    expect(find.text('May 20, 2025'), findsOneWidget);
    expect(find.text('Clean coffee maker'), findsOneWidget);

    await tester.tap(find.byTooltip('Next day'));
    await tester.pumpAndSettle();

    expect(find.text('May 21, 2025'), findsOneWidget);
    expect(find.text('Clean coffee maker'), findsNothing);

    await tester.tap(find.byTooltip('Previous day'));
    await tester.pumpAndSettle();

    expect(find.text('May 20, 2025'), findsOneWidget);
    expect(find.text('Clean coffee maker'), findsOneWidget);
  });

  testWidgets('date picker jumps directly to the chosen date', (tester) async {
    await _pumpPlanApp(tester);

    await tester.tap(find.byTooltip('Choose date'));
    await tester.pumpAndSettle();

    expect(find.byType(DatePickerDialog), findsOneWidget);

    await tester.tap(find.text('15').last);
    await tester.pumpAndSettle();
    await tester.tap(find.text('OK'));
    await tester.pumpAndSettle();

    expect(find.text('May 15, 2025'), findsOneWidget);
    expect(find.text('Clean coffee maker'), findsNothing);
  });

  testWidgets('date label scrolls timeline instead of opening picker', (
    tester,
  ) async {
    await _pumpPlanApp(tester);

    await tester.tap(find.byKey(const ValueKey('top-bar-date-button')));
    await tester.pumpAndSettle();

    expect(find.byType(DatePickerDialog), findsNothing);
    expect(find.text('May 20, 2025'), findsOneWidget);
  });

  testWidgets('open modal closes when changing dates', (tester) async {
    await _pumpPlanApp(tester);

    await tester.ensureVisible(find.byKey(const ValueKey('pay-bill')));
    await tester.tap(find.byKey(const ValueKey('pay-bill')));
    await tester.pumpAndSettle();

    expect(find.byKey(const ValueKey('task-selection-modal')), findsOneWidget);

    await tester.tap(find.byTooltip('Next day'));
    await tester.pumpAndSettle();

    expect(find.byKey(const ValueKey('task-selection-modal')), findsNothing);
    expect(find.text('May 21, 2025'), findsOneWidget);
  });
}

/// Pumps the FocusFlow app at the desktop test size.
Future<void> _pumpPlanApp(WidgetTester tester) async {
  await tester.binding.setSurfaceSize(const Size(1586, 992));
  addTearDown(() => tester.binding.setSurfaceSize(null));
  await tester.pumpWidget(FocusFlowApp(composition: _composition()));
  await tester.pumpAndSettle();
}

/// Builds the seeded demo composition used by date navigation tests.
DemoSchedulerComposition _composition() {
  return DemoSchedulerComposition.seeded(selectedDate: CivilDate(2025, 5, 20));
}
