import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DeviceInfo {
  Future<Map<String, dynamic>> getDeviceInfo() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    Map<String, dynamic> info;
    String? currentDeviceId;

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      currentDeviceId = androidInfo.id; // ✅ Device ID for Android
      info = {
        "device": "Android",
        "modules": androidInfo.model,
        "manufacturer": androidInfo.manufacturer,
        "androidVersion": androidInfo.version.release,
        "deviceId": currentDeviceId,
      };
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      currentDeviceId = iosInfo.identifierForVendor; // ✅ Device ID for iOS
      info = {
        "device": "iOS",
        "name": iosInfo.name,
        "systemName": iosInfo.systemName,
        "systemVersion": iosInfo.systemVersion,
        "modules": iosInfo.model,
        "identifierForVendor": currentDeviceId,
      };
    } else {
      info = {"device": "Unknown"};
    }

    SharedPreferences prefs = await SharedPreferences.getInstance();
    // Save full info as string (optional)
    prefs.setString('deviceInfo', info.toString());

    // ✅ Save only current device ID
    if (currentDeviceId != null) {
      prefs.setString('currentDeviceId', currentDeviceId);
    }

    return info;
  }
}
