import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/app.dart';
import 'package:tane/i18n/strings.g.dart';

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

void main() {
  testWidgets(
    'quick-add: empty label errors; filling it saves and lists the seed',
    (tester) async {
      LocaleSettings.setLocaleSync(AppLocale.en);
      final db = newTestDatabase();
      addTearDown(db.close);
      final repo = newTestRepository(db);

      await tester.pumpWidget(
        TranslationProvider(
          child: TaneApp(
            repository: repo,
            species: newTestSpeciesRepository(db),
            onboarding: newTestOnboardingStore(),
          ),
        ),
      );
      await tester.pumpAndSettle();

      // Home → inventory.
      await tester.tap(find.byKey(const Key('home.inventory')));
      await tester.pumpAndSettle();

      // Starts empty.
      expect(
        find.text('No seeds yet. Tap + to add your first.'),
        findsOneWidget,
      );

      // Open the quick-add sheet.
      await tester.tap(find.byKey(const Key('inventory.addFab')));
      await tester.pumpAndSettle();

      // Saving with an empty label surfaces the validation message.
      await tester.tap(find.byKey(const Key('quickAdd.save')));
      await tester.pumpAndSettle();
      expect(find.text('Give it a name'), findsOneWidget);

      // Enter a name and save.
      await tester.enterText(
        find.byKey(const Key('quickAdd.labelField')),
        'Grandma tomato',
      );
      await tester.tap(find.byKey(const Key('quickAdd.save')));
      await tester.pumpAndSettle();

      // Sheet closed and the seed shows in the list.
      expect(find.byKey(const Key('quickAdd.labelField')), findsNothing);
      expect(find.text('Grandma tomato'), findsOneWidget);
      await disposeTree(tester);
    },
  );

  testWidgets(
    'save & add another keeps the sheet open and accumulates a count',
    (tester) async {
      LocaleSettings.setLocaleSync(AppLocale.en);
      final db = newTestDatabase();
      addTearDown(db.close);
      final repo = newTestRepository(db);

      await tester.pumpWidget(
        TranslationProvider(
          child: TaneApp(
            repository: repo,
            species: newTestSpeciesRepository(db),
            onboarding: newTestOnboardingStore(),
          ),
        ),
      );
      await tester.pumpAndSettle();
      await tester.tap(find.byKey(const Key('home.inventory')));
      await tester.pumpAndSettle();
      await tester.tap(find.byKey(const Key('inventory.addFab')));
      await tester.pumpAndSettle();

      // First seed via "save & add another": the sheet stays open.
      await tester.enterText(
        find.byKey(const Key('quickAdd.labelField')),
        'Alpha',
      );
      await tester.tap(find.byKey(const Key('quickAdd.saveAndAddAnother')));
      await tester.pumpAndSettle();
      expect(find.byKey(const Key('quickAdd.labelField')), findsOneWidget);
      expect(find.text('1 added'), findsOneWidget);

      // The field was cleared; add a second one.
      await tester.enterText(
        find.byKey(const Key('quickAdd.labelField')),
        'Beta',
      );
      await tester.tap(find.byKey(const Key('quickAdd.saveAndAddAnother')));
      await tester.pumpAndSettle();
      expect(find.text('2 added'), findsOneWidget);

      // A third one via plain Save closes the sheet.
      await tester.enterText(
        find.byKey(const Key('quickAdd.labelField')),
        'Gamma',
      );
      await tester.tap(find.byKey(const Key('quickAdd.save')));
      await tester.pumpAndSettle();
      expect(find.byKey(const Key('quickAdd.labelField')), findsNothing);

      // All three landed in the inventory.
      expect(await db.select(db.varieties).get(), hasLength(3));
      await disposeTree(tester);
    },
  );
}
