import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:get/get.dart';
import 'package:mocktail/mocktail.dart';
import 'package:rasadyar_app/main.dart' as app;
import 'package:rasadyar_core/core.dart';

class MockGService extends Mock implements GService {}
class MockTokenStorageService extends Mock implements TokenStorageService {}

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('App Integration Tests', () {
    late MockGService mockGService;
    late MockTokenStorageService mockTokenService;

    setUp(() {
      mockGService = MockGService();
      mockTokenService = MockTokenStorageService();

      // Setup mock behaviors
      when(() => mockGService.init()).thenAnswer((_) async {});
      when(() => mockGService.isFirstTime()).thenReturn(false);
      when(() => mockGService.setIsNotFirstTime()).thenAnswer((_) async {});
      when(() => mockTokenService.init()).thenAnswer((_) async {});

      // Put mocks in GetX
      Get.put<GService>(mockGService);
      Get.put<TokenStorageService>(mockTokenService);
    });

    tearDown(() {
      Get.reset();
    });

    testWidgets('should navigate through splash to modules page', (WidgetTester tester) async {
      // Start the app
      app.main();
      await tester.pumpAndSettle();

      // Verify splash screen is displayed
      expect(find.byType(Scaffold), findsOneWidget);

      // Wait for splash screen animations and navigation
      await tester.pumpAndSettle(const Duration(seconds: 3));

      // Should navigate to modules page or auth page based on authentication state
      expect(find.byType(Scaffold), findsOneWidget);
    });

    testWidgets('should display correct app title and theme', (WidgetTester tester) async {
      // Start the app
      app.main();
      await tester.pumpAndSettle();

      // Verify app structure
      final materialApp = tester.widget<GetMaterialApp>(find.byType(GetMaterialApp));
      expect(materialApp.title, 'رصدیار');
      expect(materialApp.locale, const Locale("fa", "IR"));
    });

    testWidgets('should handle navigation between pages', (WidgetTester tester) async {
      // Start the app
      app.main();
      await tester.pumpAndSettle();

      // Wait for initial navigation
      await tester.pumpAndSettle(const Duration(seconds: 2));

      // Verify that navigation works
      expect(find.byType(Scaffold), findsOneWidget);
    });
  });
}
