import PocketBase from 'pocketbase';

const POCKETBASE_URL = process.env['POCKETBASE_URL'] ?? '';
const POCKETBASE_ADMIN_EMAIL = process.env['POCKETBASE_ADMIN_EMAIL'] ?? '';
const POCKETBASE_ADMIN_PASSWORD = process.env['POCKETBASE_ADMIN_PASSWORD'] ?? '';

const pb = new PocketBase(POCKETBASE_URL);

export async function getPocketBase(): Promise<PocketBase> {
  if (!pb.authStore.isValid) {
    await pb.admins.authWithPassword(POCKETBASE_ADMIN_EMAIL, POCKETBASE_ADMIN_PASSWORD);
  }
  return pb;
}
