"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var emailOtpService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.emailOtpService = void 0;
const common_1 = require("@nestjs/common");
const mailer_1 = require("@nestjs-modules/mailer");
let emailOtpService = emailOtpService_1 = class emailOtpService {
    get isBypassMode() {
        return (process.env.BYPASS_EMAIL_OTP ?? 'false') === 'true';
    }
    constructor(mailerService) {
        this.mailerService = mailerService;
        this.logger = new common_1.Logger(emailOtpService_1.name);
    }
    async sendOtp(email, otp) {
        this.logger.log(`SERVICE RECEIVED EMAIL: ${email}`);
        if (!email || email.trim() === '') {
            this.logger.error('EMAIL IS EMPTY IN SERVICE!');
            throw new Error('Email is missing');
        }
        if (this.isBypassMode) {
            this.logger.warn(`[BYPASS_EMAIL_OTP] Skipping email to ${email}. OTP: ${otp}`);
            return { bypassed: true, otp };
        }
        return await this.mailerService.sendMail({
            to: email,
            subject: 'Your OTP Code',
            template: `Your OTP for verifying your account is Cars24x7: ${otp}. It will expire in 10 minutes. Don't share this code with anyone.`,
            context: { otp },
            text: `Your OTP for verifying your account is Cars24x7: ${otp}. It will expire in 10 minutes. Don't share this code with anyone.`,
        });
    }
    async sendLinkForgotPassword(email, link) {
        this.logger.log(`SERVICE RECEIVED EMAIL: ${email}`);
        if (!email || email.trim() === '') {
            this.logger.error('EMAIL IS EMPTY IN SERVICE!');
            throw new Error('Email is missing');
        }
        if (this.isBypassMode) {
            this.logger.warn(`[BYPASS_EMAIL_OTP] Skipping forgot-password email to ${email}. Link: ${link}`);
            return { bypassed: true, link };
        }
        return await this.mailerService.sendMail({
            to: email,
            subject: 'Forgot Password Link',
            template: `Your password reset link is: ${link}. It will expire in 15 minutes. Don't share this link with anyone.`,
            context: { link },
            text: `Your password reset link is: ${link}. It will expire in 15 minutes. Don't share this link with anyone.`,
        });
    }
    async sendMakeVendorEmail(email, link) {
        this.logger.log(`SERVICE RECEIVED EMAIL: ${email}`);
        if (!email || email.trim() === '') {
            this.logger.error('EMAIL IS EMPTY IN SERVICE!');
            throw new Error('Email is missing');
        }
        if (this.isBypassMode) {
            this.logger.warn(`[BYPASS_EMAIL_OTP] Skipping vendor email to ${email}. Link: ${link}`);
            return { bypassed: true, link };
        }
        const message = `
Hello,
 Your vendor account has been successfully created.
 Please use the link below to log in:
Login Link: ${link}
 Username: (same as your user login)
 Password: (same as your user login)
If you need any help, feel free to contact us.
 Welcome to Cars 24/7!`;
        return await this.mailerService.sendMail({
            to: email,
            subject: 'Your Cars 24/7 Vendor Login Details',
            template: message,
            context: { link },
            text: message,
        });
    }
};
exports.emailOtpService = emailOtpService;
exports.emailOtpService = emailOtpService = emailOtpService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [mailer_1.MailerService])
], emailOtpService);
//# sourceMappingURL=otp-email.service.js.map