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_core/core.dart';

class MockTokenStorageService extends Mock implements TokenStorageService {}

class MockSliderLogic extends GetxController with Mock implements SliderLogic {}

void main() {
  group('ModulesLogic Tests', () {
    late ModulesLogic modulesLogic;
    late MockTokenStorageService mockTokenService;
    late MockSliderLogic mockUpSlider;
    late MockSliderLogic mockDownSlider;

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

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

      modulesLogic = ModulesLogic();
    });

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

    test('should initialize with correct default values', () {
      // Assert
      expect(modulesLogic.isLoading.value, false);
      expect(modulesLogic.latestNews.value, null);
      expect(modulesLogic.moduleList, isNotEmpty);
      expect(modulesLogic.moduleList.length, greaterThan(0));
    });

    test('should have correct module list structure', () {
      // Assert
      expect(modulesLogic.moduleList, isA<List<ModuleModel>>());

      // Check that chicken module exists
      final chickenModule = modulesLogic.moduleList.firstWhere(
        (module) => module.module == Module.chicken,
      );
      expect(chickenModule.title, 'رصدطیور');
      expect(chickenModule.borderColor, Color(0xFF4665AF));

      // Check that inspection module exists
      final inspectionModule = modulesLogic.moduleList.firstWhere(
        (module) => module.module == Module.inspection,
      );
      expect(inspectionModule.title, 'رصدبان');
      expect(inspectionModule.borderColor, Color(0xFF014856));
    });

    test('should update isLoading correctly', () {
      // Act
      modulesLogic.isLoading.value = true;

      // Assert
      expect(modulesLogic.isLoading.value, true);

      // Act
      modulesLogic.isLoading.value = false;

      // Assert
      expect(modulesLogic.isLoading.value, false);
    });

    test('should update latestNews correctly', () {
      // Act
      modulesLogic.latestNews.value = 'Test news';

      // Assert
      expect(modulesLogic.latestNews.value, 'Test news');

      // Act
      modulesLogic.latestNews.value = null;

      // Assert
      expect(modulesLogic.latestNews.value, null);
    });

    test('should have livestock module with correct properties', () {
      // Assert
      final livestockModule = modulesLogic.moduleList.firstWhere(
        (module) => module.module == Module.liveStocks,
      );
      expect(livestockModule.title, 'رصدام');
      expect(livestockModule.borderColor, Color(0xFFD7A972));
      expect(livestockModule.backgroundColor, Color(0xFFF4F1EF));
      expect(livestockModule.titleColor, Color(0xFF7F7F7F));
    });

    test('should have all required modules in moduleList', () {
      // Assert
      final moduleTypes = modulesLogic.moduleList.map((m) => m.module).toList();

      expect(moduleTypes.contains(Module.chicken), true);
      expect(moduleTypes.contains(Module.liveStocks), true);
      expect(moduleTypes.contains(Module.inspection), true);
    });

    test('should access slider controllers correctly', () {
      // Assert
      expect(modulesLogic.upSlider, isA<SliderLogic>());
      expect(modulesLogic.downSlider, isA<SliderLogic>());
    });

    test('should access token service correctly', () {
      // Assert
      expect(modulesLogic.tokenService, isA<TokenStorageService>());
    });
  });
}
