import 'package:alt_client/src/app/app.dart';
import 'package:alt_client/src/integrations/socket/socket_connection_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class FakeSocketConnectionController extends SocketConnectionController {
  final SocketConnectionState initialState;

  FakeSocketConnectionController(this.initialState);

  @override
  SocketConnectionState build() {
    return initialState;
  }
}

void main() {
  testWidgets(
    'shows ALT dashboard shell with default disconnected socket state',
    (tester) async {
      await tester.pumpWidget(const ProviderScope(child: AltClientApp()));

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

  testWidgets('shows ALT dashboard with socket state Connecting', (
    tester,
  ) async {
    await tester.pumpWidget(
      ProviderScope(
        overrides: [
          socketConnectionControllerProvider.overrideWith(
            () => FakeSocketConnectionController(
              SocketConnectionState.connecting,
            ),
          ),
        ],
        child: const AltClientApp(),
      ),
    );

    expect(find.text('Socket Contract'), findsOneWidget);
    expect(find.text('Connecting'), findsOneWidget);
  });

  testWidgets('shows ALT dashboard with socket state Connected', (
    tester,
  ) async {
    await tester.pumpWidget(
      ProviderScope(
        overrides: [
          socketConnectionControllerProvider.overrideWith(
            () =>
                FakeSocketConnectionController(SocketConnectionState.connected),
          ),
        ],
        child: const AltClientApp(),
      ),
    );

    expect(find.text('Socket Contract'), findsOneWidget);
    expect(find.text('Connected'), findsOneWidget);
  });

  testWidgets('shows ALT dashboard with socket state Error', (tester) async {
    await tester.pumpWidget(
      ProviderScope(
        overrides: [
          socketConnectionControllerProvider.overrideWith(
            () => FakeSocketConnectionController(SocketConnectionState.error),
          ),
        ],
        child: const AltClientApp(),
      ),
    );

    expect(find.text('Socket Contract'), findsOneWidget);
    expect(find.text('Error'), findsOneWidget);
  });

  testWidgets('opens ALT agent dock from the right rail', (tester) async {
    await tester.pumpWidget(const ProviderScope(child: AltClientApp()));

    await tester.tap(find.byIcon(Icons.smart_toy_outlined));
    await tester.pump();

    expect(find.text('Ask the ALT agent'), findsOneWidget);
    expect(find.textContaining('ALT agent shell is ready'), findsOneWidget);
  });

  testWidgets('switches ALT workbench center content from the right rail', (
    tester,
  ) async {
    await tester.pumpWidget(const ProviderScope(child: AltClientApp()));

    await tester.tap(find.byIcon(Icons.candlestick_chart_outlined));
    await tester.pump();

    expect(find.text('Markets'), findsWidgets);
    expect(find.textContaining('Market data import'), findsOneWidget);
  });
}
