import { Injectable, Logger } from '@nestjs/common';

export interface DeliveryResult {
  /** The attempt did not error. */
  ok: boolean;
  /** Whether a provider actually took the message and will deliver it. False
   *  for the log channel — nothing left this machine. */
  delivered: boolean;
  error?: string;
}

/**
 * Pluggable delivery. With no provider configured this is the "log channel": it
 * writes the message to the server log so the whole event → template → outbox
 * pipeline can be exercised, and reports delivered:false so the outbox records
 * SKIPPED rather than claiming the message was sent.
 *
 * That distinction matters more than it looks. The outbox is what staff would
 * cite in a dispute over whether a tenant was told about a debit note, so a row
 * saying SENT at a precise time, for a message that never left the building, is
 * a stored falsehood. A real EMAIL/SMS provider slots in behind this same
 * signature and returns delivered:true; nothing else changes.
 */
@Injectable()
export class ChannelDispatcher {
  private readonly logger = new Logger('Notify');

  async send(channel: string, to: string, subject: string | null, body: string): Promise<DeliveryResult> {
    const preview = body.replace(/\s+/g, ' ').slice(0, 160);
    this.logger.log(`[${channel}] → ${to}${subject ? ` | ${subject}` : ''} | ${preview}`);
    return { ok: true, delivered: false, error: 'no delivery provider configured — logged only' };
  }
}
