import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import * as random_string from 'randomstring'
// import { ModelService } from 'src/model/model.service';
import { ConfigService } from '@nestjs/config';
import * as nodemailer from "nodemailer";
import { CommonService } from './common.service';
import * as fs from 'fs'
import * as path from 'path'
import { log } from 'console';
// import {} from '../../public/templates/'
import moment from 'moment';
import { config } from 'dotenv'
import { ModelService } from '../model/model.service';

config()
@Injectable()
export class EmailService {
    // private nodemailer_email
    // private nodemailer_password
    constructor(
        private readonly CommonService: CommonService,
        // private readonly Model: ModelService,
        // private readonly ConfigService:ConfigService
    ) {

    }

    send_welcome_email = async (user_dto: any) => {
        try {
            let { email, name } = user_dto;
            // console.log("user_dto", user_dto);

            let file_path = path.resolve(__dirname, '../../templates', 'welcome.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = name != null ? name : "User";
            let subject = `Welcome to Abundish - Get Started Today!`;
            html = html.replace('%USER_NAME1%', set_name)
            html = html.replace('%USER_NAME2%', set_name)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }


    account_deactivate_email = async (user_dto: any, reason: string) => {
        try {
            let { email, name } = user_dto;
            // console.log("user_dto", user_dto);

            let file_path = path.resolve(__dirname, '../../templates', 'account_block.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = name != null ? name : "User";
            let subject = `Deactivation of Your Abundish Store Account`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%REASON%', reason)
            html = html.replace('%OUR_EMAIL%', process.env.NODEMAILER_MAIL)


            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    account_activate_email = async (user_dto: any) => {
        try {
            let { email, name } = user_dto;
            // console.log("user_dto", user_dto);

            let file_path = path.resolve(__dirname, '../../templates', 'account_unblock.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = name != null ? name : "User";
            let subject = `Your Account Has Been Unblocked / Activated`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%OUR_EMAIL%', process.env.NODEMAILER_MAIL)


            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }
    account_block_email = async (user_dto: any, reason: string) => {
        try {
            let { email, name } = user_dto;
            // console.log("user_dto", user_dto);

            let file_path = path.resolve(__dirname, '../../templates', 'account_block.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = name != null ? name : "User";
            let subject = `Deactivation of Your Abundish Store Account`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%REASON%', reason)
            html = html.replace('deactivated', 'block')
            html = html.replace('deactivation', 'block')
            html = html.replace('Deactivation', 'block')
            html = html.replace('%OUR_EMAIL%', process.env.NODEMAILER_MAIL)


            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_welcome_email_chef = async (user_dto: any) => {
        try {
            let { email, name } = user_dto;
            // console.log("user_dto", user_dto);

            let file_path = path.resolve(__dirname, '../../templates', 'welcome_chef.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = name != null ? name : "User";
            let subject = `Welcome to Abundish - Get Started Today!`;
            html = html.replace('%USER_NAME1%', set_name)
            html = html.replace('%USER_NAME2%', set_name)
            html = html.replace('Abundish Store', "Chef App")
            html = html.replace('Abundish Store', "Chef App")
            html = html.replace('Abundish Store', "Chef App")
            html = html.replace('Abundish Store', "Chef App")
            html = html.replace('Abundish Store', "Chef App")

            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_crop_schedule_email = async (user_dto: any) => {
        try {
            let { email, name } = user_dto;
            // console.log("user_dto", user_dto);

            let file_path = path.resolve(__dirname, '../../templates', 'welcome_chef.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = name != null ? name : "farmer";
            let subject = `Your Crop Pickup is Scheduled – Driver Arriving Today`;
            html = html.replace('%FARMER_NAME%', set_name)

            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_driver_approve_email = async (payload: any) => {
        try {
            const { name, email } = payload;
            const subject = "Your Driver Profile Has Been Approved!"
            const set_name = name != null ? name : "User";

            let file_path = path.resolve(__dirname, '../../templates', 'driver_approve.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            html = html.replace('%USER_NAME%', set_name)

            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_reject_email = async (payload: any) => {
        try {
            let { email, reject_reason } = payload;
            // console.log("user_dto", payload);

            let file_path = path.resolve(__dirname, '../../templates', 'driver_reject.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            // let set_name = name != null ? name : "User";
            let subject = reject_reason;
            html = html.replace('%USER_NAME1%', subject)
            // html = html.replace('%USER_NAME2%', reject_reason)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    driver_agent = async (payload: any) => {
        try {
            let { email, password, name } = payload;
            // console.log("user_dto", payload);

            let file_path = path.resolve(__dirname, '../../templates', 'driver_agent.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let subject = `Welcome to Abundish - Get Started Today!`;
            html = html.replace('%USER_NAME%', name)
            html = html.replace('%EMAIL%', email)
            html = html.replace('%PASSWORD%', password)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    farmer_agent = async (payload: any) => {
        try {
            let { email, password, name } = payload;
            // console.log("user_dto", payload);

            let file_path = path.resolve(__dirname, '../../templates', 'farmer_agent.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let subject = `Welcome to Abundish - Get Started Today!`;
            html = html.replace('%USER_NAME%', name)
            html = html.replace('%EMAIL%', email)
            html = html.replace('%PASSWORD%', password)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_welcome_email_driver = async (payload: any) => {
        try {
            let { email, pass, name } = payload;
            // console.log("user_dto", payload);
            // console.log("email_id-------->", email);
            let file_path = path.resolve(__dirname, '../../templates', 'driver.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let subject = `Welcome to Abundish - Get Started Today!`;
            html = html.replace('%PASSWORD%', pass)
            await this.CommonService.send_email_driver(email, subject, html, pass);
        }
        catch (err) {
            throw err;
        }
    }

    send_otp_to_user = async (user_dto: any) => {
        try {
            let { email, email_otp, name: user_name } = user_dto;
            let digits = email_otp.toString().split('');

            // let subject = `Your OTP for Together We Can Registration`;
            let subject = `Your OTP for Abundish  Registration`;

            let file_path = path.resolve(__dirname, '../../templates', 'email_verification.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            html = html.replace('%USER_NAME%', user_name);
            html = html.replace('%OTP_1%', digits[0]);
            html = html.replace('%OTP_2%', digits[1]);
            html = html.replace('%OTP_3%', digits[2]);
            html = html.replace('%OTP_4%', digits[3]);
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_otp_to_user_1 = async (user_dto: any) => {
        try {
            // console.log(user_dto, "userdot000000000000000000000000000");

            let { email_1, email_otp_1, name: user_name } = user_dto;
            let digits = email_otp_1.toString().split('');

            // let subject = `Your OTP for Together We Can Registration`;
            let subject = `Your OTP for Abundish  Registration`;

            let file_path = path.resolve(__dirname, '../../templates', 'email_verification.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            html = html.replace('%USER_NAME%', user_name);
            html = html.replace('%OTP_1%', digits[0]);
            html = html.replace('%OTP_2%', digits[1]);
            html = html.replace('%OTP_3%', digits[2]);
            html = html.replace('%OTP_4%', digits[3]);
            await this.CommonService.send_email(email_1, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    resend_otp_email = async (user_dto: any) => {
        try {
            let { email, email_otp, name: user_name, email_1, email_otp_1 } = user_dto;
            // console.log("email_otp---->", email_otp);
            // console.log("email---->", email);

            // Determine which OTP and email to use
            let otp_to_use = email_otp || email_otp_1;
            let email_to_use = email_otp ? email : email_1;

            let digits = otp_to_use.toString().split('');
            // let subject = `Your New OTP for Together We Can Registration`;
            let subject = `Your OTP for Abundish`;

            let file_path = path.resolve(__dirname, '../../templates', 'email_verification.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            html = html.replace('%USER_NAME%', user_name);
            html = html.replace('%OTP_1%', digits[0]);
            html = html.replace('%OTP_2%', digits[1]);
            html = html.replace('%OTP_3%', digits[2]);
            html = html.replace('%OTP_4%', digits[3]);
            await this.CommonService.send_email(email_to_use, subject, html);
        } catch (err) {
            throw err;
        }
    }



    // resend_otp_email = async (user_dto:any) => {
    //     try {
    //         let { email, email_otp,name:user_name,email_1,email_otp_1 } = user_dto;
    //         console.log("email_otp---->",email_otp);
    //         console.log("email---->",email);           
    //         let digits = email_otp.toString().split('');
    //         let subject = `Your New OTP for Together We Can Registration`;
    //         let file_path = path.resolve(__dirname, '../../templates', 'email_verification.html');
    //         let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
    //         html = html.replace('%USER_NAME%', user_name);
    //         html = html.replace('%OTP_1%', digits[0]);
    //         html = html.replace('%OTP_2%', digits[1]);
    //         html = html.replace('%OTP_3%', digits[2]);
    //         html = html.replace('%OTP_4%', digits[3]);
    //          await this.CommonService.send_email(email, subject, html);
    //     }
    //      catch (err) {
    //         throw err;
    //     }
    // }

    resend_edit_email_otp_email = async (user_dto: any) => {
        try {
            let { email_1, email_otp_1, name: user_name } = user_dto;
            let digits = email_otp_1.toString().split('');

            // let subject = `Your New OTP for Together We Can Registration`;
            let subject = `Your OTP for Abundish`;
            let file_path = path.resolve(__dirname, '../../templates', 'email_verification.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            html = html.replace('%USER_NAME%', user_name);
            html = html.replace('%OTP_1%', digits[0]);
            html = html.replace('%OTP_2%', digits[1]);
            html = html.replace('%OTP_3%', digits[2]);
            html = html.replace('%OTP_4%', digits[3]);
            await this.CommonService.send_email(email_1, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    forgot_password_mail = async (user_dto: any) => {
        let { email, email_otp, name } = user_dto;
        // let subject = `Reset Your Password - Together We Can`;
        let subject = `Reset Your Password - Abundish`;

        try {
            let file_path = path.resolve(__dirname, '../../templates', 'reset_password.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            let digits = email_otp.toString().split('');

            html = html.replace('%USER_NAME%', name);
            html = html.replace('%OTP_1%', digits[0]);
            html = html.replace('%OTP_2%', digits[1]);
            html = html.replace('%OTP_3%', digits[2]);
            html = html.replace('%OTP_4%', digits[3]);
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    resend_forgot_password_mail = async (user_dto: any) => {
        let { email, forgot_password_otp, user_name } = user_dto;
        let subject = `Resend OTP to Reset Your Password - Together We Can`;

        try {
            let file_path = path.resolve(__dirname, '../../templates', 'reset_password.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            let digits = forgot_password_otp.toString().split('');

            html = html.replace('%USER_NAME%', user_name);
            html = html.replace('%OTP_1%', digits[0]);
            html = html.replace('%OTP_2%', digits[1]);
            html = html.replace('%OTP_3%', digits[2]);
            html = html.replace('%OTP_4%', digits[3]);
            // await this.CommonService.send_email(email, subject, html)
        }
        catch (err) {
            throw err;
        }
    }


    send_pickup_decline_email = async (email: any, user_name: any) => {
        try {
            let file_path = path.resolve(__dirname, '../../templates', 'pickup_reject.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name ? user_name : "User";
            let subject = `Unable to Pick Up at Your Address - Please Select a Drop-Off Location`;
            html = html.replace('%USER_NAME%', set_name)
            // html = html.replace('%USER_NAME2%', set_name)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_pickup_accept_email = async (email: any, user_name: any) => {
        try {
            let file_path = path.resolve(__dirname, '../../templates', 'pickup_accept.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name ? user_name : "User";
            let subject = `Pickup Address Accepted - Awaiting Driver Confirmation`;
            html = html.replace('%USER_NAME%', set_name)
            // html = html.replace('%USER_NAME2%', set_name)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }


    send_contact_us_resolve_email = async (email: any, user_name: any) => {
        try {
            let file_path = path.resolve(__dirname, '../../templates', 'contact_us_resolve.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name ? user_name : "User";
            let subject = `Your Query Has Been Resolved`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%OUR_EMAIL%', process.env.NODEMAILER_MAIL)
            // html = html.replace('%USER_NAME2%', set_name)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    send_contact_us_reject_email = async (email: any, user_name: any, reason: any) => {
        try {
            let file_path = path.resolve(__dirname, '../../templates', 'contact_us_reject.html');
            // console.log("file_path...", file_path)
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name ? user_name : "User";
            let subject = `Request Declined by Admin - Important Information`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%OUR_EMAIL%', process.env.NODEMAILER_MAIL)
            html = html.replace('%REASON%', reason)
            // html = html.replace('%USER_NAME2%', set_name)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    order_confirm_email = async (email: any, user_name: any, order_data: any) => {
        try {
            let { order_id, created_at } = order_data
            let file_path = path.resolve(__dirname, '../../templates', 'order_confirmation.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name || "User";
            let subject = `Order Confirmation – Thank You for Your Purchase!`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%ORDER_ID%', order_id)
            html = html.replace('%ORDER_DATE%', moment(created_at).format())
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    order_delivered_email = async (email: any, user_name: any, order_data: any) => {
        try {
            let { order_id, created_at } = order_data
            let file_path = path.resolve(__dirname, '../../templates', 'order_delivered.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name || "User";
            let subject = `Your Order Has Been Delivered!`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%ORDER_ID%', order_id)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }


    order_refund_email = async (email: any, user_name: any, order_data: any) => {
        try {
            let { order_id, total_prices } = order_data
            let file_path = path.resolve(__dirname, '../../templates', 'refund.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name || "User";
            let subject = `Refund initiated!`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%ORDER_ID%', order_id)
            html = html.replace('%REFUND_AMOUNT%', String(Math.round((Number(total_prices) + Number.EPSILON) * 100) / 100))

            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    earnings_email = async (email: any, user_name: any, amount: any) => {
        try {
            let file_path = path.resolve(__dirname, '../../templates', 'earnings_receive.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name || "User";
            let subject = `Refund initiated!`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%AMOUNT%', amount)
            html = html.replace('%DATE%', moment().format())

            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }


    order_decline_chef_email = async (email: any, user_name: any, order_data: any) => {
        try {
            let { order_id } = order_data
            let file_path = path.resolve(__dirname, '../../templates', 'order_decline.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });

            let set_name = user_name || "User";
            let subject = `Your Order Has Been decline!`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%ORDER_ID%', order_id)
            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

    crop_schedule_email = async (email: any, user_name: string, location: string, date: any, crop_data: any) => {
        try {
            let { schedule_type, name: crop_name } = crop_data
            let file_path = path.resolve(__dirname, '../../templates', 'crop_schedule.html');
            let html = fs.readFileSync(file_path, { encoding: 'utf-8' });
            let set_name = user_name || "User";
            let subject = `Your Order Has Been decline!`;
            html = html.replace('%USER_NAME%', set_name)
            html = html.replace('%CROP_NAME%', crop_name)
            html = html.replace('%CROP_TYPE%', schedule_type)
            html = html.replace('%DATE%', date)
            html = html.replace('%LOCATION%', location)

            await this.CommonService.send_email(email, subject, html);
        }
        catch (err) {
            throw err;
        }
    }

}
