import { Injectable } from '@nestjs/common';
import { NotificationProvider, NotificationChannel } from './notification-provider.interface';
import { EmailProvider } from './providers/email.provider';
import { WhatsappProvider } from './providers/whatsapp.provider';

@Injectable()
export class NotificationService {
  private providers: Record<NotificationChannel, NotificationProvider> = {} as any;

  constructor() {
    // Registramos proveedores por defecto (puede reemplazarse desde el código de inicialización)
    this.registerProvider('email', new EmailProvider());
    this.registerProvider('whatsapp', new WhatsappProvider());
  }

  registerProvider(channel: NotificationChannel, provider: NotificationProvider) {
    this.providers[channel] = provider;
  }

  async send(
    channel: NotificationChannel,
    to: string,
    subject: string,
    body: string,
    metadata?: any,
  ) {
    const provider = this.providers[channel];
    if (!provider) {
      throw new Error(`No provider registered for channel ${channel}`);
    }
    await provider.send({ to, subject, body, metadata });
  }
}
