import { Platform } from 'react-native';
import { CREATE_TABLES } from './schema';

let db: any | null = null;

export async function getDatabase(): Promise<any> {
  if (db) return db;

  // expo-sqlite doesn't support web — skip DB init on web
  if (Platform.OS === 'web') {
    console.log('SQLite not available on web, using in-memory stub');
    db = null;
    return db;
  }

  const SQLite = await import('expo-sqlite');
  db = await SQLite.openDatabaseAsync('wifaq.db');
  await db.execAsync(CREATE_TABLES);
  return db;
}

export function generateId(): string {
  return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
