import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/core/contracts/library.dart';
import 'package:smplayer/features/aggregated/widgets/aggregated_media_card.dart';

void main() {
  testWidgets('multi-source badge does not trigger the whole card', (
    tester,
  ) async {
    var cardTapCount = 0;
    var sourceBadgeTapCount = 0;

    await tester.pumpWidget(
      ProviderScope(
        child: MaterialApp(
          home: Scaffold(
            body: Center(
              child: SizedBox(
                width: 420,
                child: AggregatedMediaCard(
                  item: const EmbyRawItem(Id: 'movie-1', Name: 'Movie'),
                  imageUrl: null,
                  title: 'Movie',
                  serverName: 'Alpha',
                  serverSourceCount: 2,
                  onTap: () => cardTapCount++,
                  onServerSourcesTap: () => sourceBadgeTapCount++,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    expect(find.text('Alpha 2服'), findsOneWidget);
    expect(find.byIcon(Icons.keyboard_arrow_down_rounded), findsOneWidget);

    await tester.tap(find.text('Alpha 2服'));
    await tester.pump();

    expect(sourceBadgeTapCount, 1);
    expect(cardTapCount, 0);

    await tester.tap(find.text('Movie'));
    await tester.pump();

    expect(cardTapCount, 1);
  });

  testWidgets('single-source badge keeps the existing passive appearance', (
    tester,
  ) async {
    var cardTapCount = 0;

    await tester.pumpWidget(
      ProviderScope(
        child: MaterialApp(
          home: Scaffold(
            body: Center(
              child: SizedBox(
                width: 420,
                child: AggregatedMediaCard(
                  item: const EmbyRawItem(Id: 'movie-1', Name: 'Movie'),
                  imageUrl: null,
                  title: 'Movie',
                  serverName: 'Alpha',
                  onTap: () => cardTapCount++,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    expect(find.text('Alpha'), findsOneWidget);
    expect(find.byIcon(Icons.keyboard_arrow_down_rounded), findsNothing);

    await tester.tap(find.text('Alpha'));
    await tester.pump();

    expect(cardTapCount, 1);
  });
}
