import * as nodemailer from 'nodemailer'

export interface EmailConfig {
  smtpHost: string
  smtpPort: number
  smtpUser: string
  smtpPass: string
  fromEmail: string
  toEmail: string
}

export class EmailService {
  private config: EmailConfig
  private transporter: nodemailer.Transporter | null = null

  constructor(config: EmailConfig) {
    this.config = config
    this.initTransporter()
  }

  updateConfig(config: EmailConfig): void {
    this.config = config
    this.initTransporter()
  }

  private initTransporter(): void {
    if (!this.config.smtpHost || !this.config.smtpUser) {
      this.transporter = null
      return
    }

    this.transporter = nodemailer.createTransport({
      host: this.config.smtpHost,
      port: this.config.smtpPort,
      secure: this.config.smtpPort === 465,
      auth: {
        user: this.config.smtpUser,
        pass: this.config.smtpPass,
      },
    })
  }

  async sendEmail(subject: string, text: string, html?: string): Promise<boolean> {
    if (!this.transporter) {
      console.warn('Email transporter not configured')
      return false
    }

    try {
      await this.transporter.sendMail({
        from: this.config.fromEmail || this.config.smtpUser,
        to: this.config.toEmail,
        subject: `[IP Monitor] ${subject}`,
        text,
        html: html || this.generateHtml(subject, text),
      })
      return true
    } catch (error) {
      console.error('Failed to send email:', error)
      return false
    }
  }

  async sendAlert(title: string, message: string): Promise<boolean> {
    return this.sendEmail(title, message)
  }

  async testConnection(): Promise<{ success: boolean; error?: string }> {
    if (!this.transporter) {
      return { success: false, error: 'Email not configured' }
    }

    try {
      await this.transporter.verify()
      return { success: true }
    } catch (error) {
      return { success: false, error: (error as Error).message }
    }
  }

  private generateHtml(title: string, content: string): string {
    return `
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <style>
          body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background-color: #0f0f12;
            color: #e4e4e7;
            padding: 20px;
          }
          .container {
            max-width: 600px;
            margin: 0 auto;
            background: #18181b;
            border-radius: 12px;
            padding: 30px;
            border: 1px solid #27272a;
          }
          h1 {
            color: #e06c75;
            border-bottom: 2px solid #e06c75;
            padding-bottom: 15px;
            margin-bottom: 20px;
          }
          .content {
            background: #1e1e22;
            padding: 20px;
            border-radius: 8px;
            border: 1px solid #27272a;
          }
          pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: 'JetBrains Mono', 'Fira Code', monospace;
            color: #a1a1aa;
          }
          .footer {
            margin-top: 20px;
            color: #71717a;
            font-size: 12px;
            text-align: center;
          }
        </style>
      </head>
      <body>
        <div class="container">
          <h1>${title}</h1>
          <div class="content">
            <pre>${content}</pre>
          </div>
          <p class="footer">
            IP Monitor - ${new Date().toLocaleString('tr-TR')}
          </p>
        </div>
      </body>
      </html>
    `
  }
}

