import 'package:alt_client/src/app/bootstrap.dart';
import 'package:alt_client/src/integrations/mattermost/mattermost_auth_service.dart';
import 'package:alt_client/src/integrations/mattermost/mattermost_push_host_integration.dart';
import 'package:alt_client/src/integrations/mattermost/mattermost_push_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

class _FakePushClient implements MattermostPushClient {
  int initializeCalls = 0;

  @override
  Stream<Map<String, dynamic>> get onNotification => const Stream.empty();

  @override
  Future<void> initialize() async {
    initializeCalls += 1;
  }

  @override
  Future<String?> getDeviceToken() async => null;

  @override
  Future<void> setAuthToken(
    String serverUrl,
    String token, {
    String? identifier,
  }) async {}

  @override
  Future<void> setSigningKey(String serverUrl, String signingKey) async {}

  @override
  set onDeviceTokenReady(Future<void> Function(String token)? callback) {}

  @override
  set onNavigateToChannel(
    void Function(String serverUrl, String channelId)? callback,
  ) {}

  @override
  set onNavigateToThread(
    void Function(String serverUrl, String rootId)? callback,
  ) {}
}

class _NoopAuthService implements MattermostAuthService {
  @override
  Future<void> autoLoginAndRegister() async {}

  @override
  noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

void main() {
  group('AltClientBootstrap Tests', () {
    test(
      'default bootstrapAltClient does not call Firebase or Mattermost and returns null',
      () async {
        final host = await bootstrapAltClient(
          options: const AltClientBootstrapOptions(applyFullscreen: false),
        );
        expect(host, isNull);
      },
    );

    test(
      'opt-in bootstrapAltClient initializes Firebase and Mattermost push host',
      () async {
        int firebaseInitCalls = 0;
        final fakePushClient = _FakePushClient();
        final host = await bootstrapAltClient(
          options: AltClientBootstrapOptions(
            enablePush: true,
            applyFullscreen: false,
            firebaseInitializer: () async {
              firebaseInitCalls += 1;
            },
            mattermostHostFactory: () {
              return MattermostPushHostIntegration(
                pushClient: fakePushClient,
                authServiceFactory: (_) => _NoopAuthService(),
              );
            },
          ),
        );

        expect(firebaseInitCalls, equals(1));
        expect(host, isNotNull);
        expect(fakePushClient.initializeCalls, equals(1));
      },
    );

    testWidgets(
      'default runAltClient renders AltClientApp shell without Firebase/push',
      (tester) async {
        Widget? capturedApp;
        await runAltClient(
          options: const AltClientBootstrapOptions(applyFullscreen: false),
          runAppOverride: (app) {
            capturedApp = app;
          },
        );

        expect(capturedApp, isNotNull);
        await tester.pumpWidget(capturedApp!);

        expect(find.text('ALT Console'), findsOneWidget);
        expect(find.text('Backtests'), findsWidgets);
        expect(find.text('Disconnected'), findsOneWidget);
      },
    );
  });
}
