import { SES, SESServiceException } from '@aws-sdk/client-ses';
import { Injectable } from '@nestjs/common';
import envConfig from 'src/common/config/envConfig';
import { emailConfig, IEmailBody } from './email.config';

@Injectable()
export class EmailService {

    constructor() { }

    onModuleInit() {
        // if user is verified for the first time, send welcome email
        //return;
        // this.sendEmailToTheBinaPlus({
        //     subject: `Ashik יצר קשר מהאתר שלך`,
        //     template: "contactUs",
        //     heading: `Ashik יצר קשר מהאתר שלך`,
        //     body: {
        //         "name": "Email title",
        //         "email": "ashik@gmail.com",
        //         "phone": "01544444444",
        //         message: '',
        //     }
        // });
    }

    public async sendEmail(params: Partial<IEmailBody> | any) {
        try {


            const sesClient = new SES({
                // The key apiVersion is no longer supported in v3, and can be removed.
                // @deprecated The client uses the "latest" apiVersion.
                // apiVersion: '2010-12-01',
                apiVersion: 'latest',

                region: `${envConfig().AWS_REGION}`,

                credentials: {
                    accessKeyId: `${envConfig().AWS_ACCESS_KEY_ID}`,
                    secretAccessKey: `${envConfig().AWS_SECRET_ACCESS_KEY}`,
                },
            });

            return await sesClient.sendEmail(emailConfig(params));
        } catch (error) {
            throw new SESServiceException(error);
        }
    }


    /* SEND EMAIL FOR Bina Plus*/
    public async sendEmailToTheBinaPlus(params) {
        try {
            const sesClient = new SES({
                // The key apiVersion is no longer supported in v3, and can be removed.
                // @deprecated The client uses the "latest" apiVersion.
                // apiVersion: '2010-12-01',
                apiVersion: 'latest',

                region: `${envConfig().AWS_REGION}`,

                credentials: {
                    accessKeyId: `${envConfig().AWS_ACCESS_KEY_ID}`,
                    secretAccessKey: `${envConfig().AWS_SECRET_ACCESS_KEY}`,
                },
            });

            return await sesClient.sendEmail(emailConfig({ ...params, toEmail: ["help@binaplus.co.il"] }));
        } catch (error) {
            console.log(error);
            throw error;
        }
    }



}
