import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:forui/forui.dart';
import 'package:smplayer/core/contracts/auth.dart';
import 'package:smplayer/features/home/widgets/server_dropdown_button.dart';
import 'package:smplayer/providers/home_page_mode_provider.dart';
import 'package:smplayer/providers/session_provider.dart';

void main() {
  testWidgets('server mode uses a capsule dropdown trigger', (tester) async {
    await _pumpButton(tester, mode: HomePageMode.server);

    expect(find.byType(BackdropFilter), findsOneWidget);
    expect(find.byType(FPopoverMenu), findsOneWidget);
    expect(find.text('Alpha'), findsOneWidget);

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

    expect(find.text('推荐'), findsOneWidget);
    expect(find.text('Beta'), findsOneWidget);
  });

  testWidgets('recommend mode uses the same capsule dropdown trigger', (
    tester,
  ) async {
    await _pumpButton(tester, mode: HomePageMode.recommend);

    expect(find.byType(BackdropFilter), findsOneWidget);
    expect(find.byType(FPopoverMenu), findsOneWidget);
    expect(find.text('推荐'), findsOneWidget);

    await tester.tap(find.text('推荐'));
    await tester.pumpAndSettle();

    expect(find.text('Alpha'), findsOneWidget);
    expect(find.text('Beta'), findsOneWidget);
  });
}

Future<void> _pumpButton(
  WidgetTester tester, {
  required HomePageMode mode,
}) async {
  await tester.pumpWidget(
    ProviderScope(
      overrides: [
        homePageModeProvider.overrideWith((_) => mode),
        sessionProvider.overrideWith(_FakeSessionNotifier.new),
      ],
      child: const MaterialApp(
        home: Scaffold(body: Center(child: ServerDropdownButton())),
      ),
    ),
  );
  await tester.pumpAndSettle();
}

class _FakeSessionNotifier extends SessionNotifier {
  @override
  Future<SessionListRes> build() async => const SessionListRes(
    activeServerId: 'alpha',
    sessions: [
      AuthedSession(
        serverId: 'alpha',
        serverName: 'Alpha',
        serverUrl: 'https://alpha.example',
        token: 'token-a',
        userId: 'user-a',
        userName: 'User A',
        isActive: true,
      ),
      AuthedSession(
        serverId: 'beta',
        serverName: 'Beta',
        serverUrl: 'https://beta.example',
        token: 'token-b',
        userId: 'user-b',
        userName: 'User B',
        isActive: false,
      ),
    ],
  );
}
