import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:iop_client/client_config.dart';
import 'package:iop_client/main.dart';
import 'support/client_test_harness.dart';

void main() {
  testWidgets('Client App basic rendering and success handshake test', (
    WidgetTester tester,
  ) async {
    final fakeClient = FakeClientWireClient(shouldSuccess: true);
    final fakeStatusRepo = FakeControlPlaneStatusRepository();

    await tester.pumpWidget(
      IopClientApp(testClient: fakeClient, statusRepository: fakeStatusRepo),
    );
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    expect(find.text('IOP CONTROL PLANE'), findsOneWidget);
    expect(find.text('Operations Overview'), findsOneWidget);

    expect(
      find.text(ClientConfig.controlPlaneHttpUrl, findRichText: true),
      findsOneWidget,
    );
    expect(
      find.text(ClientConfig.controlPlaneWireUrl, findRichText: true),
      findsOneWidget,
    );

    expect(find.text('CONNECTED'), findsOneWidget);
  });

  testWidgets('Client App connection error state test', (
    WidgetTester tester,
  ) async {
    final fakeClient = FakeClientWireClient(
      shouldSuccess: false,
      mockMessage: 'Invalid Version',
    );
    final fakeStatusRepo = FakeControlPlaneStatusRepository();

    await tester.pumpWidget(
      IopClientApp(testClient: fakeClient, statusRepository: fakeStatusRepo),
    );
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    expect(find.text('ERROR'), findsOneWidget);
  });

  testWidgets('Client App has no standalone Agent rail button or surface', (
    WidgetTester tester,
  ) async {
    final fakeClient = FakeClientWireClient(shouldSuccess: true);
    final fakeStatusRepo = FakeControlPlaneStatusRepository();

    await tester.pumpWidget(
      IopClientApp(testClient: fakeClient, statusRepository: fakeStatusRepo),
    );
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    expect(find.byTooltip('Agent'), findsNothing);
    expect(find.text('Ask about IOP operations'), findsNothing);
    expect(find.textContaining('IOP agent surface is ready'), findsNothing);
  });

  testWidgets(
    'Client App mobile screen layout verification for layout and overflow',
    (WidgetTester tester) async {
      // Set a small mobile screen size
      tester.view.physicalSize = const Size(360, 640);
      tester.view.devicePixelRatio = 1.0;

      final fakeClient = FakeClientWireClient(shouldSuccess: true);
      final fakeStatusRepo = FakeControlPlaneStatusRepository();

      await tester.pumpWidget(
        IopClientApp(testClient: fakeClient, statusRepository: fakeStatusRepo),
      );
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 100));

      // Overview
      expect(find.text('Operations Overview'), findsOneWidget);

      // Switch to Edges panel
      await tester.tap(find.byTooltip('Edges'));
      await tester.pump();
      expect(find.text('Edge Alpha'), findsNWidgets(2));

      // Switch to Runtime panel
      await tester.tap(find.byTooltip('Runtime'));
      await tester.pump();
      expect(find.text('Operations'), findsOneWidget);

      // Reset view size after test
      addTearDown(() {
        tester.view.resetPhysicalSize();
        tester.view.resetDevicePixelRatio();
      });
    },
  );
}
