import 'server-only';

import { SupabaseClient } from '@supabase/supabase-js';

import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';

type Notification = Database['medreport']['Tables']['notifications'];

export function createNotificationsService(client: SupabaseClient<Database>) {
  return new NotificationsService(client);
}

class NotificationsService {
  constructor(private readonly client: SupabaseClient<Database>) {}

  async createNotification(params: Notification['Insert']) {
    const logger = await getLogger();
    const { error } = await this.client
      .schema('medreport')
      .from('notifications')
      .insert(params);

    if (error) {
      logger.error(
        { ...params },
        `Could not create notification: ${error.message}`,
      );
      throw error;
    }
  }
}
