import 'package:afterwork/data/services/preferences_helper.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  group('PreferencesHelper', () {
    late PreferencesHelper preferencesHelper;

    setUp(() async {
      // Clear SharedPreferences before each test
      SharedPreferences.setMockInitialValues({});
      preferencesHelper = PreferencesHelper();
    });

    group('setString', () {
      test('should save string value successfully', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        const key = 'test_key';
        const value = 'test_value';

        // Act
        await preferencesHelper.setString(key, value);

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString(key), value);
      });

      test('should overwrite existing value', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({'test_key': 'old_value'});
        const key = 'test_key';
        const newValue = 'new_value';

        // Act
        await preferencesHelper.setString(key, newValue);

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString(key), newValue);
      });
    });

    group('getString', () {
      test('should return saved string value', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({'test_key': 'test_value'});
        const key = 'test_key';
        // Recreate helper to get fresh instance
        preferencesHelper = PreferencesHelper();

        // Act
        final result = await preferencesHelper.getString(key);

        // Assert
        expect(result, 'test_value');
      });

      test('should return null for non-existent key', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        const key = 'non_existent_key';

        // Act
        final result = await preferencesHelper.getString(key);

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

    group('remove', () {
      test('should remove key successfully', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({'test_key': 'test_value'});
        const key = 'test_key';

        // Act
        await preferencesHelper.remove(key);

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString(key), isNull);
      });

      test('should handle removing non-existent key gracefully', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        const key = 'non_existent_key';

        // Act & Assert
        await preferencesHelper.remove(key);
        // Should not throw
      });
    });

    group('saveUserId', () {
      test('should save userId successfully', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        const userId = 'user123';

        // Act
        await preferencesHelper.saveUserId(userId);

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString('user_id'), userId);
      });
    });

    group('getUserId', () {
      test('should return saved userId', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({'user_id': 'user123'});
        preferencesHelper = PreferencesHelper(); // Recreate to get fresh instance

        // Act
        final result = await preferencesHelper.getUserId();

        // Assert
        expect(result, 'user123');
      });

      test('should return null when userId not found', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        preferencesHelper = PreferencesHelper();

        // Act
        final result = await preferencesHelper.getUserId();

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

    group('saveUserName', () {
      test('should save userName successfully', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        const userName = 'John';

        // Act
        await preferencesHelper.saveUserName(userName);

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString('user_name'), userName);
      });
    });

    group('getUseFirstrName', () {
      test('should return saved firstName', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({'user_name': 'John'});
        preferencesHelper = PreferencesHelper(); // Recreate to get fresh instance

        // Act
        final result = await preferencesHelper.getUseFirstrName();

        // Assert
        expect(result, 'John');
      });

      test('should return null when firstName not found', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        preferencesHelper = PreferencesHelper();

        // Act
        final result = await preferencesHelper.getUseFirstrName();

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

    group('saveUserLastName', () {
      test('should save lastName successfully', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        const lastName = 'Doe';

        // Act
        await preferencesHelper.saveUserLastName(lastName);

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString('user_last_name'), lastName);
      });
    });

    group('getUserLastName', () {
      test('should return saved lastName', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({'user_last_name': 'Doe'});
        preferencesHelper = PreferencesHelper(); // Recreate to get fresh instance

        // Act
        final result = await preferencesHelper.getUserLastName();

        // Assert
        expect(result, 'Doe');
      });

      test('should return null when lastName not found', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});
        preferencesHelper = PreferencesHelper();

        // Act
        final result = await preferencesHelper.getUserLastName();

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

    group('clearUserInfo', () {
      test('should remove all user info keys', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({
          'user_id': '123',
          'user_name': 'John',
          'user_last_name': 'Doe',
        });

        // Act
        await preferencesHelper.clearUserInfo();

        // Assert
        final prefs = await SharedPreferences.getInstance();
        expect(prefs.getString('user_id'), isNull);
        expect(prefs.getString('user_name'), isNull);
        expect(prefs.getString('user_last_name'), isNull);
      });

      test('should handle clearing when no user info exists', () async {
        // Arrange
        SharedPreferences.setMockInitialValues({});

        // Act & Assert
        await preferencesHelper.clearUserInfo();
        // Should not throw
      });
    });
  });
}

