import PocketBase from 'pocketbase';

const PB_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
const PB_ADMIN_EMAIL = import.meta.env.PB_ADMIN_EMAIL || 'admin@example.com';
const PB_ADMIN_PASSWORD = import.meta.env.PB_ADMIN_PASSWORD || 'admin_password';

const pbServer = new PocketBase(PB_URL);

export { pbServer };

export async function initPbServer() {
  if (!pbServer.authStore.isValid) {
    await pbServer.collection('_superusers').authWithPassword(PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD);
  }
  return pbServer;
}