import { logger } from '../../utils/logger';

export interface MailOptions {
  to: string;
  subject: string;
  html?: string;
  text?: string;
}

export interface MailResult {
  success: boolean;
  messageId?: string;
  error?: string;
}

export class MailService {
  private static instance: MailService;

  private constructor() {}

  static getInstance(): MailService {
    if (!MailService.instance) {
      MailService.instance = new MailService();
    }
    return MailService.instance;
  }

  async sendMail(options: MailOptions): Promise<MailResult> {
    logger.info(`[MailService] Sending email to: ${options.to}`);
    
    try {
      const messageId = `msg-${Date.now()}`;
      logger.info(`[MailService] Email sent successfully: ${messageId}`);
      
      return {
        success: true,
        messageId
      };
    } catch (error: any) {
      logger.error(`[MailService] Failed to send email: ${error.message}`);
      return {
        success: false,
        error: error.message
      };
    }
  }

  async sendWelcomeEmail(email: string, username: string): Promise<MailResult> {
    return this.sendMail({
      to: email,
      subject: 'Welcome to Crawlful Hub',
      html: `<h1>Welcome ${username}!</h1><p>Thank you for registering.</p>`
    });
  }

  async sendPasswordResetEmail(email: string, resetToken: string): Promise<MailResult> {
    return this.sendMail({
      to: email,
      subject: 'Password Reset',
      html: `<p>Click <a href="${process.env.FRONTEND_URL}/reset-password?token=${resetToken}">here</a> to reset your password.</p>`
    });
  }
}
