import { createDbService } from '@/server/services/db.js';
/**
 * Push client factory - convenience module for constructing a PushClient
 * from Cloudflare Workers env bindings.
 *
 * Other modules import `getPushClient(env)` to get a ready-to-use PushClient.
 */

import { createPushClient } from './send.js';
import type { PushClient } from './types.js';

export interface PushEnvBindings {
  DATABASE_URL: string;
  VAPID_PUBLIC_KEY: string;
  VAPID_PRIVATE_KEY: string;
  VAPID_SUBJECT: string;
}

/**
 * Returns a PushClient bound to the given env bindings.
 * Used by API routes and other server modules that need to send push notifications.
 */
export function getPushClient(env: PushEnvBindings): PushClient {
  const db = createDbService({ DATABASE_URL: env.DATABASE_URL });
  return createPushClient(db, {
    VAPID_PUBLIC_KEY: env.VAPID_PUBLIC_KEY,
    VAPID_PRIVATE_KEY: env.VAPID_PRIVATE_KEY,
    VAPID_SUBJECT: env.VAPID_SUBJECT,
    DATABASE_URL: env.DATABASE_URL,
  });
}
