import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:recipe_flutter/features/inventory/domain/inventory_item.dart';
import 'package:recipe_flutter/features/inventory/presentation/swipeable_inventory_tile.dart';

Widget _wrap(Widget child) {
  return ProviderScope(
    child: MaterialApp(
      home: Scaffold(body: child),
    ),
  );
}

void main() {
  testWidgets('shows deepest category in chip and full path in tooltip', (
    tester,
  ) async {
    const item = InventoryItem(
      id: 1,
      productId: 99,
      productName: 'Jordgubbssylt',
      productCanonicalName: 'Jordgubbssylt',
      categoryPath: 'Skafferi > Sylt, mos & marmelad > Sylt',
      quantity: 1,
      unit: 'st',
      opened: false,
    );

    await tester.pumpWidget(_wrap(const SwipeableInventoryTile(item: item)));

    expect(find.text('Sylt'), findsOneWidget);
    final tooltipFinder = find.ancestor(
      of: find.byType(Chip).first,
      matching: find.byType(Tooltip),
    );
    final tooltip = tester.widget<Tooltip>(tooltipFinder);
    expect(tooltip.message, 'Skafferi > Sylt, mos & marmelad > Sylt');
  });

  testWidgets('falls back to L1 when category path is missing', (tester) async {
    const item = InventoryItem(
      id: 2,
      productId: 100,
      productName: 'Okategoriserad vara',
      productCanonicalName: 'Okategoriserad vara',
      categoryPath: null,
      quantity: 2,
      unit: 'st',
      opened: false,
    );

    await tester.pumpWidget(_wrap(const SwipeableInventoryTile(item: item)));

    expect(find.text('Övrigt'), findsOneWidget);
    final tooltipFinder = find.ancestor(
      of: find.byType(Chip).first,
      matching: find.byType(Tooltip),
    );
    final tooltip = tester.widget<Tooltip>(tooltipFinder);
    expect(tooltip.message, 'Övrigt');
  });
}
