import { Injectable } from '@nestjs/common';
import * as nodeMailer from 'nodemailer'

@Injectable()
export class MailerService {
    private async transporter(){
        const testAccount = await nodeMailer.createTestAccount()
        const transport = nodeMailer.createTransport({
            host: '127.0.0.1',
            port:1025,
            ignoreTLS:true,
            auth: {
                user: testAccount.user,
                pass: testAccount.pass
            }
        })

        return transport
    }

    async sendMail(userEmail: string){
        (await this.transporter()).sendMail({
            from:'app@localhost.com',
            to:userEmail,
            subject: 'Inscription',
            html:'<h1>Confirmation of inscription</h1>'
        })
    }

    async sendTesetPwd(email: string, url: string, code: string){
        (await this.transporter()).sendMail({
            from:'app@localhost.com',
            to:email,
            subject: 'Reset pwd',
            html:`
                <a href="${url}">reset pwd</a>
                <p>Secret code <strong>${code}</strong></p>
                <p>Secret code expire in 15 mns</p>
            `
        })
    }
}
