import { Injectable, Logger } from "@nestjs/common";
import { NotificationStrategy } from "./notification.strategy";
import { EmailClientService } from "../email-client.service";

@Injectable()
export class EmailNotificationStrategy implements NotificationStrategy {
    private readonly logger = new Logger(EmailNotificationStrategy.name);
    constructor(private readonly emailClient: EmailClientService) { }
    async send(recipient: string, message: string): Promise<boolean> {
        try {
            // Route through the shared email client (RabbitMQ hand-off). `queued`
            // reflects whether the message was accepted for delivery; a false or
            // a thrown result is a real failure the caller must observe.
            const { queued } = await this.emailClient.sendEmail({
                to: recipient,
                subject: "EDR Freight notification",
                text: message,
            });
            return queued;
        } catch (err) {
            this.logger.error(
                `Failed to send email to ${recipient}: ${err instanceof Error ? err.message : String(err)}`,
                err instanceof Error ? err.stack : undefined,
            );
            return false;
        }
    }
}
