import { Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as Application from 'expo-application';

const STORAGE_KEY = 'rebreak_device_id';

let cached: string | null = null;

export async function getDeviceId(): Promise<string> {
  if (cached) return cached;

  if (Platform.OS === 'ios') {
    const vendor = await Application.getIosIdForVendorAsync();
    if (vendor) {
      cached = vendor;
      return vendor;
    }
  }

  if (Platform.OS === 'android') {
    const androidId = Application.getAndroidId();
    if (androidId) {
      cached = androidId;
      return androidId;
    }
  }

  // Fallback: persisted UUID via AsyncStorage (web / simulator edge cases)
  const stored = await AsyncStorage.getItem(STORAGE_KEY).catch(() => null);
  if (stored) {
    cached = stored;
    return stored;
  }

  const uuid =
    'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
      const r = (Math.random() * 16) | 0;
      return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
    });

  await AsyncStorage.setItem(STORAGE_KEY, uuid).catch(() => {});
  cached = uuid;
  return uuid;
}

export function getPlatformName(): string {
  if (Platform.OS === 'ios') return 'ios';
  if (Platform.OS === 'android') return 'android';
  return 'web';
}
