import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:health_app/widgets/common_widgets.dart';

void main() {
  testWidgets('swipe row paints an opaque white content surface', (
    tester,
  ) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: SwipeDeleteTile(
            onDelete: () {},
            child: const SizedBox(height: 72, child: Text('用药一')),
          ),
        ),
      ),
    );

    final surface = tester.widget<ColoredBox>(
      find.byKey(const ValueKey('swipe-delete-content-surface')),
    );
    expect(surface.color, Colors.white);
  });

  testWidgets('swipe reveals a dedicated delete action', (tester) async {
    var deleteCount = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: SwipeDeleteTile(
            onDelete: () => deleteCount++,
            child: const SizedBox(
              height: 72,
              width: double.infinity,
              child: Text('报告一'),
            ),
          ),
        ),
      ),
    );

    expect(find.byKey(const ValueKey('swipe-delete-action')), findsNothing);
    await tester.drag(find.text('报告一'), const Offset(-100, 0));
    await tester.pumpAndSettle();

    expect(find.byKey(const ValueKey('swipe-delete-action')), findsOneWidget);
    await tester.tap(find.byKey(const ValueKey('swipe-delete-action')));
    await tester.pumpAndSettle();

    expect(deleteCount, 1);
  });

  testWidgets('tapping an opened row closes it without deleting', (
    tester,
  ) async {
    var deleteCount = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: SwipeDeleteTile(
            onDelete: () => deleteCount++,
            child: const SizedBox(
              height: 72,
              width: double.infinity,
              child: Text('用药一'),
            ),
          ),
        ),
      ),
    );

    await tester.drag(find.text('用药一'), const Offset(-100, 0));
    await tester.pumpAndSettle();
    await tester.tapAt(const Offset(100, 40));
    await tester.pumpAndSettle();

    expect(deleteCount, 0);
  });
}
