import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/db/database.dart';
import 'package:tane/db/enums.dart';
import 'package:tane/ui/inventory_list_screen.dart';

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

void main() {
  late AppDatabase db;

  setUp(() => db = newTestDatabase());
  tearDown(() => db.close());

  testWidgets('marking a lot to give away shows on its tile', (tester) async {
    final repo = newTestRepository(db);
    final id = await repo.addQuickVariety(label: 'Maize');
    await repo.addLot(varietyId: id, harvestYear: 2024);

    await tester.pumpWidget(
      wrapDetail(
        repository: repo,
        varietyId: id,
        species: newTestSpeciesRepository(db),
      ),
    );
    await tester.pumpAndSettle();

    await tester.tap(find.textContaining('Year 2024')); // open the lot
    await tester.pumpAndSettle();

    await tester.ensureVisible(find.byKey(const Key('lot.addShare')));
    await tester.pumpAndSettle();
    await tester.tap(find.byKey(const Key('lot.addShare')));
    await tester.pumpAndSettle();

    await tester.ensureVisible(find.byKey(const Key('share.shared')));
    await tester.tap(find.byKey(const Key('share.shared')));
    await tester.pumpAndSettle();

    await tester.ensureVisible(find.byKey(const Key('addLot.save')));
    await tester.tap(find.byKey(const Key('addLot.save')));
    await tester.pumpAndSettle();

    // The lot line now carries the share terms.
    expect(find.textContaining('To give away'), findsOneWidget);
    await disposeTree(tester);
  });

  testWidgets('declaring plenty reveals the share choice with a nudge', (
    tester,
  ) async {
    final repo = newTestRepository(db);
    final id = await repo.addQuickVariety(label: 'Maize');

    await tester.pumpWidget(
      wrapDetail(
        repository: repo,
        varietyId: id,
        species: newTestSpeciesRepository(db),
      ),
    );
    await tester.pumpAndSettle();

    await tester.tap(find.byKey(const Key('detail.addLot')));
    await tester.pumpAndSettle();

    // Reveal abundance and declare plenty.
    await tester.ensureVisible(find.byKey(const Key('lot.addAbundance')));
    await tester.tap(find.byKey(const Key('lot.addAbundance')));
    await tester.pumpAndSettle();
    await tester.ensureVisible(
      find.byKey(const Key('abundance.plentyToShare')),
    );
    await tester.tap(find.byKey(const Key('abundance.plentyToShare')));
    await tester.pumpAndSettle();

    // The share section reveals itself, still private, with a soft nudge.
    expect(find.byKey(const Key('share.private')), findsOneWidget);
    expect(find.byKey(const Key('share.nudge')), findsOneWidget);
    await disposeTree(tester);
  });

  testWidgets('the sharing filter narrows the list to what I share', (
    tester,
  ) async {
    final repo = newTestRepository(db);
    final sharedId = await repo.addQuickVariety(label: 'Shared bean');
    await repo.addLot(varietyId: sharedId, offerStatus: OfferStatus.exchange);
    final privateId = await repo.addQuickVariety(label: 'Private bean');
    await repo.addLot(varietyId: privateId);

    await tester.pumpWidget(
      wrapScreen(repository: repo, child: const InventoryListScreen()),
    );
    await tester.pumpAndSettle();

    // Both listed; the shared one carries its badge, and the print action and
    // filter chip are offered.
    expect(find.text('Shared bean'), findsOneWidget);
    expect(find.text('Private bean'), findsOneWidget);
    expect(find.byKey(Key('inventory.shared.$sharedId')), findsOneWidget);
    expect(find.byKey(const Key('inventory.printCatalog')), findsOneWidget);

    await tester.tap(find.byKey(const Key('inventory.filter.sharing')));
    await tester.pumpAndSettle();

    expect(find.text('Shared bean'), findsOneWidget);
    expect(find.text('Private bean'), findsNothing);
    await disposeTree(tester);
  });
}
