import 'package:drift/drift.dart' show Value;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/domain/crop_calendar.dart';
import 'package:tane/i18n/strings.g.dart';
import 'package:tane/ui/calendar_screen.dart';

import '../support/test_support.dart';

/// The "what's due this month" screen groups varieties by phase for the picked
/// month, and switching months changes what's shown.
void main() {
  testWidgets('shows a variety under Sow for its recorded month, hidden in others',
      (tester) async {
    final db = newTestDatabase();
    addTearDown(db.close);
    final repo = newTestRepository(db);
    final id = await repo.addQuickVariety(label: 'Rúcula');
    await repo.updateVariety(id: id, sowMonths: Value(monthsToMask([3])));

    await tester.pumpWidget(wrapScreen(
      repository: repo,
      locale: AppLocale.es,
      child: const CalendarScreen(initialMonth: 3),
    ));
    await tester.pumpAndSettle();

    expect(find.byKey(Key('calendar.entry.$id')), findsOneWidget);
    expect(find.text('Rúcula'), findsOneWidget);

    // Jump to a month it doesn't sow in → empty state, entry gone.
    await tester.tap(find.byKey(const Key('calendar.month.6')));
    await tester.pumpAndSettle();
    expect(find.byKey(Key('calendar.entry.$id')), findsNothing);

    await disposeTree(tester);
  });

  testWidgets('an empty month shows the "nothing" message', (tester) async {
    final db = newTestDatabase();
    addTearDown(db.close);
    final repo = newTestRepository(db);
    final id = await repo.addQuickVariety(label: 'Tomate');
    await repo.updateVariety(id: id, sowMonths: Value(monthsToMask([4])));

    await tester.pumpWidget(wrapScreen(
      repository: repo,
      locale: AppLocale.es,
      child: const CalendarScreen(initialMonth: 1), // nothing in January
    ));
    await tester.pumpAndSettle();

    expect(find.byKey(Key('calendar.entry.$id')), findsNothing);
    expect(find.byIcon(Icons.event_available), findsOneWidget);

    await disposeTree(tester);
  });
}
