import { BaseMail } from '@adonisjs/mail'
import type User from '#models/user'
import i18nManager from '@adonisjs/i18n/services/main'
import { resetPasswordHtml } from './templates/reset_password.js'

export default class PasswordResetNotification extends BaseMail {
  readonly #resetUrl: string
  readonly #locale: string

  constructor(user: User, resetUrl: string, locale: string = 'en') {
    super()
    this.user = user
    this.#resetUrl = resetUrl
    this.#locale = locale
  }

  private user: User

  prepare() {
    const i18n = i18nManager.locale(this.#locale)
    const t = (key: string, data?: Record<string, unknown>) => i18n.t(`mails.${key}`, data)

    this.message
      .to(this.user.email)
      .subject(t('password_reset.subject') as string)
      .html(resetPasswordHtml(t, this.user.username ?? '', this.#resetUrl))
  }
}
