"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var EmailTemplateService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailTemplateService = void 0;
const common_1 = require("@nestjs/common");
const sequelize_1 = require("@nestjs/sequelize");
const sequelize_2 = require("sequelize");
const ai_service_1 = require("../ai/ai.service");
const email_template_entity_1 = require("./entities/email-template.entity");
const email_landing_page_entity_1 = require("../email-landing-page/entities/email-landing-page.entity");
const email_template_clue_entity_1 = require("../email-template-clues/entities/email-template-clue.entity");
const email_template_clues_service_1 = require("../email-template-clues/email-template-clues.service");
const email_landing_page_service_1 = require("../email-landing-page/email-landing-page.service");
let EmailTemplateService = EmailTemplateService_1 = class EmailTemplateService {
    constructor(emailTemplateModel, aiService, landingPageService, emailTemplateCluesService) {
        this.emailTemplateModel = emailTemplateModel;
        this.aiService = aiService;
        this.landingPageService = landingPageService;
        this.emailTemplateCluesService = emailTemplateCluesService;
        this.logger = new common_1.Logger(EmailTemplateService_1.name);
    }
    async create(data, businessId) {
        return this.emailTemplateModel.create({ ...data, businessId: businessId });
    }
    async createFromJson(data, businessId) {
        for (const element of data) {
            const l = await this.landingPageService.create({
                content: element.landingPageContent,
                domain: "",
                name: "default"
            }, businessId);
            this.logger.log("data");
            const email = await this.emailTemplateModel.create({ ...element, templateComplexity: this.getComplexity(element.templateComplexity), businessId: businessId, landingPageId: l.id });
            await this.emailTemplateCluesService.createMany(element.emailPhishingClues, email.id);
        }
        return;
    }
    getComplexity(complexity) {
        if (complexity === 'Easy') {
            return 'low';
        }
        else if (complexity === 'Medium') {
            return 'medium';
        }
        else if (complexity === 'Complex') {
            return 'high';
        }
        else {
            return 'unknown';
        }
    }
    async autogenerateEmailTemplateFromPrompt(data, businessId) {
        if (!data) {
            throw new common_1.NotFoundException('Prompt is required for email template autogeneration.');
        }
        const email = await this.aiService.autogenerateEmailTemplateFromPrompt(data.prompt);
        return email;
    }
    async findAll(businessId) {
        this.logger.log('emailTemplateModel.findAll', businessId);
        return this.emailTemplateModel.findAll({
            where: {
                [sequelize_2.Op.or]: [{ businessId: businessId }, { templateType: 'system' }],
            }, include: [email_landing_page_entity_1.EmailLandingPage, email_template_clue_entity_1.EmailTemplateClue]
        });
    }
    async findOne(id, businessId) {
        const template = await this.emailTemplateModel.findOne({
            where: {
                id: id,
                businessId: businessId
            }
        });
        if (!template) {
            throw new common_1.NotFoundException(`Email template with ID ${id} not found`);
        }
        return template;
    }
    async findByDomain(domainName, businessId) {
        const template = await this.emailTemplateModel.findAll({
            where: {
                templateDomain: domainName,
                businessId: businessId
            }
        });
        return template;
    }
    async update(id, data, businessId) {
        const template = await this.findOne(id, businessId);
        this.logger.log('update', data);
        this.emailTemplateCluesService.updateTemplateClues(id, data.emailPhishingClues);
        return template.update({ ...data, businessId });
    }
    async remove(id, businessId) {
        const template = await this.findOne(id, businessId);
        await template.destroy();
    }
};
exports.EmailTemplateService = EmailTemplateService;
exports.EmailTemplateService = EmailTemplateService = EmailTemplateService_1 = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, sequelize_1.InjectModel)(email_template_entity_1.EmailTemplate)),
    __metadata("design:paramtypes", [Object, ai_service_1.AiService,
        email_landing_page_service_1.EmailLandingPageService,
        email_template_clues_service_1.EmailTemplateCluesService])
], EmailTemplateService);
//# sourceMappingURL=email-template.service.js.map