export type PlatformType = 'web' | 'desktop' | 'mobile';

declare global {
  interface Window {
    __TAURI__?: unknown;
    flutter_inappwebview?: unknown;
    Capacitor?: unknown;
  }
}

interface PlatformConfig {
  platform: PlatformType;
  apiBaseUrl: string;
  isSecureContext: boolean;
  supportsNotifications: boolean;
  supportsFileSystem: boolean;
}

let cachedPlatform: PlatformType | null = null;

export function detectPlatform(): PlatformType {
  // Always re-check unless we've confirmed a native environment
  if (cachedPlatform === 'mobile' || cachedPlatform === 'desktop') {
    return cachedPlatform;
  }
  
  if (typeof window === 'undefined') {
    console.log('[Platform] Detected: web (no window)');
    return 'web';
  }
  
  // Check for specific native bridges
  if (window.__TAURI__ !== undefined) {
    console.log('[Platform] Detected: desktop (Tauri)');
    cachedPlatform = 'desktop';
    return cachedPlatform;
  }
  
  // Capacitor check - sometimes injected late, so don't cache 'web' result immediately
  if (window.Capacitor !== undefined) {
    console.log('[Platform] Detected: mobile (Capacitor)');
    cachedPlatform = 'mobile';
    return cachedPlatform;
  }

  // Flutter check
  if (window.flutter_inappwebview !== undefined) {
    console.log('[Platform] Detected: mobile (Flutter)');
    cachedPlatform = 'mobile';
    return cachedPlatform;
  }
  
  const userAgent = navigator.userAgent.toLowerCase();
  
  if (userAgent.includes('electron')) {
    console.log('[Platform] Detected: desktop (Electron)');
    cachedPlatform = 'desktop';
    return cachedPlatform;
  }
  
  if (userAgent.includes('cordova')) {
    console.log('[Platform] Detected: mobile (Cordova)');
    cachedPlatform = 'mobile';
    return cachedPlatform;
  }

  // Fallback: Check for Android/iOS in User Agent if Capacitor isn't ready yet
  if (userAgent.includes('android') || userAgent.includes('iphone') || userAgent.includes('ipad') || userAgent.includes('vortex')) {
     console.log('[Platform] Detected: mobile (UA override)', userAgent);
     // We don't cache this as 'mobile' permanently yet in case it's just a mobile browser, 
     // but for this specific hybrid app, treating it as mobile is safer.
     return 'mobile'; 
  }
  
  // Default to web, but do NOT cache it so we can re-check later when Capacitor might be ready
  console.log('[Platform] Detected: web (default check)');
  return 'web';
}

function getApiBaseUrl(): string {
  const platform = detectPlatform();
  
  if (platform === 'web') {
    return '';
  }
  
  const envUrl = import.meta.env.VITE_API_BASE_URL;
  if (envUrl) return envUrl;
  
  return 'https://aethex.network';
}

export function getPlatformConfig(): PlatformConfig {
  const platform = detectPlatform();
  
  return {
    platform,
    apiBaseUrl: getApiBaseUrl(),
    isSecureContext: typeof window !== 'undefined' && window.isSecureContext,
    supportsNotifications: typeof Notification !== 'undefined',
    supportsFileSystem: typeof window !== 'undefined' && 'showOpenFilePicker' in window,
  };
}

export function isDesktop(): boolean {
  return detectPlatform() === 'desktop';
}

export function isMobile(): boolean {
  return detectPlatform() === 'mobile';
}

export function isWeb(): boolean {
  return detectPlatform() === 'web';
}

export const platformConfig = getPlatformConfig();
