import { Injectable, type OnModuleInit } from '@nestjs/common';
import * as nodemailer from 'nodemailer';
import { LoggerService } from '@modules/shared';

export interface SendEmailDto {
  to: string;
  subject: string;
  html: string;
}

@Injectable()
export class EmailService implements OnModuleInit {
  private transporter!: nodemailer.Transporter;

  constructor(private readonly logger: LoggerService) {}

  onModuleInit(): void {
    const host = process.env['SMTP_HOST'] ?? 'localhost';
    const port = Number(process.env['SMTP_PORT'] ?? '1025');
    const user = process.env['SMTP_USER'];
    const pass = process.env['SMTP_PASS'];

    this.transporter = nodemailer.createTransport({
      host,
      port,
      secure: port === 465,
      ...(user && pass ? { auth: { user, pass } } : {}),
    });

    this.logger.log(`Email transport configured: ${host}:${port}`, 'EmailService');
  }

  async send(dto: SendEmailDto): Promise<{ messageId: string }> {
    const from = process.env['SMTP_FROM'] ?? 'noreply@goodgo.vn';

    try {
      const info = await this.transporter.sendMail({
        from,
        to: dto.to,
        subject: dto.subject,
        html: dto.html,
      });

      this.logger.log(`Email sent successfully: ${info.messageId}`, 'EmailService');
      return { messageId: info.messageId };
    } catch (error) {
      this.logger.error(
        `Failed to send email: ${error instanceof Error ? error.message : String(error)}`,
        'EmailService',
      );
      throw error;
    }
  }

  async verify(): Promise<boolean> {
    try {
      await this.transporter.verify();
      return true;
    } catch {
      return false;
    }
  }
}
