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/presentation/pages/modules/view.dart';
import 'package:rasadyar_app/presentation/pages/modules/logic.dart';
import 'package:rasadyar_core/core.dart';

class MockModulesLogic extends GetxController with Mock implements ModulesLogic {}
class MockTokenStorageService extends Mock implements TokenStorageService {}
class MockSliderLogic extends GetxController with Mock implements SliderLogic {}

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('Modules Page Integration Tests', () {
    late MockModulesLogic mockModulesLogic;
    late MockTokenStorageService mockTokenService;
    late MockSliderLogic mockUpSlider;
    late MockSliderLogic mockDownSlider;

    setUp(() {
      mockModulesLogic = MockModulesLogic();
      mockTokenService = MockTokenStorageService();
      mockUpSlider = MockSliderLogic();
      mockDownSlider = MockSliderLogic();

      // Setup mock behaviors
      mockModulesLogic.isLoading = false.obs;
      mockModulesLogic.moduleList = [
        ModuleModel(
          title: 'رصدطیور',
          icon: 'test_icon.svg',
          module: Module.chicken,
          borderColor: Color(0xFF4665AF),
          backgroundColor: Color(0xFFECEEF2),
          titleColor: Color(0xFF4665AF),
        ),
        ModuleModel(
          title: 'رصدبان',
          icon: 'test_icon2.svg',
          module: Module.inspection,
          borderColor: Color(0xFF014856),
          backgroundColor: Color(0xFFE9EDED),
          titleColor: Color(0xFF014856),
        ),
      ];

      Get.put<ModulesLogic>(mockModulesLogic);
      Get.put<TokenStorageService>(mockTokenService);
      Get.put<SliderLogic>(mockUpSlider, tag: "up");
      Get.put<SliderLogic>(mockDownSlider, tag: "down");
    });

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

    testWidgets('should display modules grid and handle user interaction', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: ModulesPage(),
        ),
      );
      await tester.pumpAndSettle();

      // Assert - Verify modules page loads correctly
      expect(find.text('سامانه جامع رصدیار'), findsOneWidget);
      expect(find.byType(AppBar), findsOneWidget);
      expect(find.byType(GridView), findsOneWidget);

      // Verify app bar styling
      final appBar = tester.widget<AppBar>(find.byType(AppBar));
      expect(appBar.backgroundColor, AppColor.blueNormal);
      expect(appBar.centerTitle, true);
    });

    testWidgets('should show loading state correctly', (WidgetTester tester) async {
      // Arrange
      mockModulesLogic.isLoading.value = true;

      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: ModulesPage(),
        ),
      );
      await tester.pump();

      // Assert - Should show loading indicator
      expect(find.byType(Container), findsWidgets);

      // Change to not loading
      mockModulesLogic.isLoading.value = false;
      await tester.pump();

      // Should show main content
      expect(find.byType(Column), findsWidgets);
    });

    testWidgets('should handle module selection', (WidgetTester tester) async {
      // Act
      await tester.pumpWidget(
        MaterialApp(
          home: ModulesPage(),
        ),
      );
      await tester.pumpAndSettle();

      // Assert - Grid should be present for module selection
      expect(find.byType(GridView), findsOneWidget);

      // Verify modules are accessible
      expect(mockModulesLogic.moduleList.length, greaterThan(0));
    });
  });
}
