import { registerPlugin } from '@capacitor/core';
import { isNativePlatform } from '../utils/capacitor';

// Bridge to the native FullScreenIntentPlugin (see
// android/app/src/main/java/chat/vojo/app/FullScreenIntentPlugin.java).
// Web and iOS builds get the no-op fallback below — FSI opt-in only exists on Android 14+.
export interface FullScreenIntentPlugin {
  canUseFullScreenIntent(): Promise<{ value: boolean }>;
  openSettings(): Promise<void>;
}

const FullScreenIntent = registerPlugin<FullScreenIntentPlugin>('FullScreenIntent', {
  web: {
    canUseFullScreenIntent: async () => ({ value: true }),
    openSettings: async () => undefined,
  },
});

export const canUseFullScreenIntent = async (): Promise<boolean> => {
  if (!isNativePlatform()) return true;
  try {
    const { value } = await FullScreenIntent.canUseFullScreenIntent();
    return value;
  } catch {
    // Plugin not yet available (e.g., old build installed before the plugin shipped).
    // Fail "allowed" so we don't nag users on older APKs — the permission is a no-op
    // on APIs that don't check it, and on APIs that do the ring itself will make the
    // problem obvious (no wakeup) and the user can find the setting themselves.
    return true;
  }
};

export const openFullScreenIntentSettings = async (): Promise<void> => {
  if (!isNativePlatform()) return;
  await FullScreenIntent.openSettings();
};
