import type { MobilePersistenceAdapter } from '../../contracts/mobile.contracts';
import { MobileSqliteConnectionService } from '../../services/mobile-sqlite-connection.service';

/**
 * Capacitor SQLite persistence adapter.
 *
 * Initializes native SQLite with schema mirrored from Electron TypeORM entities.
 * Domain persistence routes through {@link CapacitorDatabaseService} on Capacitor shells.
 */
export class CapacitorMobilePersistenceAdapter implements MobilePersistenceAdapter {
  private initialized = false;

  constructor(private readonly connection: MobileSqliteConnectionService) {}

  get isNativeSqlite(): boolean {
    return this.connection.isAvailable;
  }

  async initialize(): Promise<void> {
    if (this.initialized) {
      return;
    }

    const store = await this.connection.initialize();

    if (!store?.isAvailable) {
      console.warn('[mobile] SQLite plugin unavailable on this Capacitor shell');
      this.initialized = true;
      return;
    }

    this.initialized = true;
    console.info('[mobile] native SQLite persistence initialized');
  }
}
