import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:mocktail/mocktail.dart';
import 'package:rasadyar_core/core.dart';

class MockHiveBox extends Mock implements Box {}

void main() {
  group('GService Tests', () {
    late GService gService;
    late MockHiveBox mockBox;

    setUp(() {
      mockBox = MockHiveBox();
      gService = GService();

      // Setup Hive mock
      when(() => mockBox.get(any())).thenReturn(null);
      when(() => mockBox.put(any(), any())).thenAnswer((_) async {});
      when(() => mockBox.delete(any())).thenAnswer((_) async {});
      when(() => mockBox.clear()).thenAnswer((_) async {});
    });

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

    test('should initialize correctly', () async {
      // Act
      await gService.init();

      // Assert
      expect(gService, isA<GService>());
    });

    test('should check first time correctly when true', () {
      // Arrange
      when(() => mockBox.get('isFirstTime')).thenReturn(true);

      // Act
      final result = gService.isFirstTime();

      // Assert
      expect(result, isTrue);
    });

    test('should check first time correctly when false', () {
      // Arrange
      when(() => mockBox.get('isFirstTime')).thenReturn(false);

      // Act
      final result = gService.isFirstTime();

      // Assert
      expect(result, isFalse);
    });

    test('should return true for first time when no value stored', () {
      // Arrange
      when(() => mockBox.get('isFirstTime')).thenReturn(null);

      // Act
      final result = gService.isFirstTime();

      // Assert
      expect(result, isTrue);
    });

    test('should set is not first time', () async {
      // Act
      await gService.setIsNotFirstTime();

      // Assert - Method should complete without error
      expect(() => gService.setIsNotFirstTime(), returnsNormally);
    });

    test('should save and retrieve target page', () {
      // Arrange
      const testPage = '/test-page';
      when(() => mockBox.get('targetPage')).thenReturn(testPage);

      // Act
      gService.saveTargetPage(testPage);
      final retrievedPage = gService.getTargetPage();

      // Assert
      expect(retrievedPage, equals(testPage));
    });

    test('should handle null target page', () {
      // Arrange
      when(() => mockBox.get('targetPage')).thenReturn(null);

      // Act
      final retrievedPage = gService.getTargetPage();

      // Assert
      expect(retrievedPage, isNull);
    });

    test('should clear target page', () {
      // Act
      gService.clearTargetPage();

      // Assert
      expect(() => gService.clearTargetPage(), returnsNormally);
    });

    test('should save and retrieve user preferences', () {
      // Arrange
      const testPreference = 'dark_mode';
      when(() => mockBox.get('userPreference')).thenReturn(testPreference);

      // Act
      gService.saveUserPreference(testPreference);
      final retrievedPreference = gService.getUserPreference();

      // Assert
      expect(retrievedPreference, equals(testPreference));
    });
  });
}
