import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:mocktail/mocktail.dart';
import 'package:rasadyar_app/presentation/pages/modules/logic.dart';
import 'package:rasadyar_app/presentation/pages/modules/view.dart';
import 'package:rasadyar_core/core.dart';

class MockModulesLogic extends GetxController with Mock implements ModulesLogic {
  @override
  RxBool isLoading = false.obs;

  @override
  List<ModuleModel> 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),
    ),
  ];
}

class MockTokenStorageService extends Mock implements TokenStorageService {}

class MockSliderLogic extends GetxController with Mock implements SliderLogic {}

void main() {
  group('ModulesPage Tests', () {
    late MockModulesLogic mockController;
    late MockTokenStorageService mockTokenService;
    late MockSliderLogic mockUpSlider;
    late MockSliderLogic mockDownSlider;

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

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

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

    testWidgets('should display app bar with correct title and logo', (WidgetTester tester) async {
      // Arrange
      mockController.isLoading.value = false;

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

      // Assert
      expect(find.byType(AppBar), findsOneWidget);
      expect(find.text('سامانه جامع رصدیار'), findsOneWidget);

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

    testWidgets('should show loading indicator when isLoading is true', (WidgetTester tester) async {
      // Arrange
      mockController.isLoading.value = true;

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

      // Assert
      expect(find.byType(Container), findsWidgets);
      // The loading container should be present
      final containers = tester.widgetList<Container>(find.byType(Container));
      expect(containers.any((container) =>
        container.color == Colors.grey.withValues(alpha: 0.5)), true);
    });

    testWidgets('should display main content when not loading', (WidgetTester tester) async {
      // Arrange
      mockController.isLoading.value = false;

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

      // Assert
      expect(find.byType(Column), findsWidgets);
      expect(find.byType(GridView), findsOneWidget);
    });

    testWidgets('should display correct number of modules in grid', (WidgetTester tester) async {
      // Arrange
      mockController.isLoading.value = false;

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

      // Assert
      expect(find.byType(GridView), findsOneWidget);
      // Grid should contain the modules from moduleList
      final gridView = tester.widget<GridView>(find.byType(GridView));
      expect(gridView, isNotNull);
    });

    testWidgets('should have correct scaffold structure', (WidgetTester tester) async {
      // Arrange
      mockController.isLoading.value = false;

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

      // Assert
      expect(find.byType(Scaffold), findsOneWidget);
      expect(find.byType(AppBar), findsOneWidget);
    });
  });
}
