import type { NotificationsRepository, EmailProvider } from '../domain/ports.js';
import type { EmailMessage } from '../domain/notification.js';

export type DispatchOutcome = 'sent' | 'duplicate';

export class NotificationsService {
  constructor(
    private readonly repo: NotificationsRepository,
    private readonly provider: EmailProvider,
  ) {}

  async dispatch(message: EmailMessage): Promise<DispatchOutcome> {
    const existing = await this.repo.findByEventId(message.eventId);
    if (existing) return 'duplicate';
    const result = await this.repo.record(message);
    if (!result.created) return 'duplicate';
    await this.provider.send(message);
    return 'sent';
  }
}
