/**
 * EncryptID - Unified Identity System for the r-Ecosystem
 *
 * A self-sovereign identity system built on WebAuthn passkeys,
 * with derived cryptographic keys and social recovery.
 *
 * @packageDocumentation
 */

// ============================================================================
// CORE MODULES
// ============================================================================

// WebAuthn/Passkey authentication
export {
  registerPasskey,
  authenticatePasskey,
  startConditionalUI,
  abortConditionalUI,
  isConditionalMediationAvailable,
  detectCapabilities,
  bufferToBase64url,
  base64urlToBuffer,
  generateChallenge,
  type EncryptIDCredential,
  type AuthenticationResult,
  type EncryptIDConfig,
  type WebAuthnCapabilities,
} from './webauthn';

// Key derivation and cryptography
export {
  EncryptIDKeyManager,
  getKeyManager,
  resetKeyManager,
  encryptData,
  decryptData,
  decryptDataAsString,
  signData,
  verifySignature,
  wrapKeyForRecipient,
  unwrapSharedKey,
  type DerivedKeys,
  type EncryptedData,
  type SignedData,
} from './key-derivation';

// Session management
export {
  SessionManager,
  getSessionManager,
  AuthLevel,
  OPERATION_PERMISSIONS,
  type EncryptIDClaims,
  type SessionState,
  type OperationPermission,
} from './session';

// Social recovery
export {
  RecoveryManager,
  getRecoveryManager,
  GuardianType,
  getGuardianTypeInfo,
  type Guardian,
  type RecoveryConfig,
  type RecoveryRequest,
} from './recovery';

// ============================================================================
// ENCRYPTED VAULT
// ============================================================================

export {
  VaultManager,
  getVaultManager,
  resetVaultManager,
  type AccountVault,
} from './vault';

// ============================================================================
// WALLET SYNC
// ============================================================================

export {
  syncWalletsOnLogin,
  syncWalletsToVault,
} from './wallet-sync';

// ============================================================================
// UI COMPONENTS
// ============================================================================

// Import UI components (registers custom elements as side effect)
export { GuardianSetupElement } from './ui/guardian-setup';
export { EncryptIDLoginButton } from './ui/login-button';

// ============================================================================
// CONVENIENCE FUNCTIONS
// ============================================================================

import { detectCapabilities } from './webauthn';
import { getSessionManager, AuthLevel } from './session';
import { getRecoveryManager } from './recovery';

/**
 * Check if EncryptID is available and ready to use
 */
export async function isEncryptIDAvailable(): Promise<boolean> {
  const caps = await detectCapabilities();
  return caps.webauthn;
}

/**
 * Get the current authentication status
 */
export function getAuthStatus(): {
  authenticated: boolean;
  did: string | null;
  authLevel: AuthLevel;
  recoveryConfigured: boolean;
} {
  const session = getSessionManager();
  const recovery = getRecoveryManager();

  return {
    authenticated: session.isValid(),
    did: session.getDID(),
    authLevel: session.getAuthLevel(),
    recoveryConfigured: recovery.isConfigured(),
  };
}

/**
 * Check if an operation is allowed with current session
 */
export function canPerformOperation(operation: string): {
  allowed: boolean;
  reason?: string;
} {
  return getSessionManager().canPerform(operation);
}

/**
 * Clear all EncryptID state (logout)
 */
export function logout(): void {
  getSessionManager().clearSession();

  // Clear keys
  const { resetKeyManager } = require('./key-derivation');
  resetKeyManager();
}

// ============================================================================
// VERSION INFO
// ============================================================================

export const VERSION = '0.1.0';
export const SPEC_VERSION = '2026-02';
