import generalTemplate from '../../src/tools/mail/templates/general.js';
import jobCompletedTemplate from '../../src/tools/mail/templates/job-completed.js';
import passwordResetTemplate from '../../src/tools/mail/templates/password-reset.js';
import performanceReportTemplate from '../../src/tools/mail/templates/performance-report.js';
import welcomeTemplate from '../../src/tools/mail/templates/welcome.js';
import workerAlertTemplate from '../../src/tools/mail/templates/worker-alert.js';
import { generateUuid, Logger, Mail, Queue } from '../../src/index.js';
import { createQueueWorker } from '../../packages/workers/src/index.js';
// Type guard function to check if argument is a Job
function isJob(arg) {
    return 'data' in arg && typeof arg.data !== 'undefined';
}
const looksLikeHtml = (value) => /<\s*html\b|<!doctype\b|<\s*body\b/i.test(value);
const resolveTemplateForRender = (template) => {
    const normalized = template
        .trim()
        .toLowerCase()
        .replace(/\.html$/, '');
    switch (normalized) {
        case 'welcome':
            return welcomeTemplate;
        case 'general':
            return generalTemplate;
        case 'job-completed':
            return jobCompletedTemplate;
        case 'password-reset':
            return passwordResetTemplate;
        case 'performance-report':
            return performanceReportTemplate;
        case 'worker-alert':
            return workerAlertTemplate;
        default:
            return template;
    }
};
function buildBaseVariables(payload, jobId) {
    const { to, subject } = payload;
    return {
        email: to ?? 'test@zintrust.com',
        subject: subject ?? 'Worker Notification',
        processed_at: new Date().toISOString(),
        job_id: jobId,
        timestamp: new Date().toISOString(),
        status: 'success',
    };
}
function buildTemplateWithCustomData(payload, baseVars) {
    return {
        template: payload.template || 'general',
        variables: {
            ...baseVars,
            ...payload.templateData,
        },
    };
}
function buildTemplateWithDefaultData(payload, baseVars) {
    const { subject, body } = payload;
    return {
        template: payload.template || 'general',
        variables: {
            ...baseVars,
            headline: subject ?? 'Worker Notification',
            message: body ?? 'Worker job completed successfully.',
            primary_color: '#3b82f6',
        },
    };
}
function buildGeneralTemplate(payload, baseVars) {
    const { subject, body } = payload;
    return {
        template: 'general',
        variables: {
            ...baseVars,
            headline: subject ?? 'Worker Job Completed',
            message: body ?? 'Worker job has been processed successfully.',
            primary_color: '#3b82f6',
            action_url: payload.templateData?.['action_url'] ?? null,
            action_text: payload.templateData?.['action_text'] ?? 'View Details',
        },
    };
}
function buildTemplateVariables(payload, jobId) {
    const baseVars = buildBaseVariables(payload, jobId);
    if (payload.template !== null && payload.template !== undefined && payload.templateData) {
        return buildTemplateWithCustomData(payload, baseVars);
    }
    if (payload.template !== null && payload.template !== undefined) {
        return buildTemplateWithDefaultData(payload, baseVars);
    }
    return buildGeneralTemplate(payload, baseVars);
}
async function sendEmail(payload, selectedTemplate, templateVars) {
    const { to, subject, body } = payload;
    const templateInput = looksLikeHtml(selectedTemplate)
        ? selectedTemplate
        : resolveTemplateForRender(selectedTemplate);
    const htmlContent = await Mail.render({
        template: templateInput,
        variables: templateVars,
    });
    const result = await Mail.send({
        to: to ?? 'test@zintrust.com',
        subject: subject ?? 'Worker Notification from ZinTrust',
        text: body ?? `Worker job completed successfully.`,
        html: htmlContent,
        from: {
            address: 'no-reply@engage.vizo.app',
            name: 'ZinTrust Advanced Worker',
        },
    });
    Logger.info('Advanced email sent', {
        template: selectedTemplate,
        to: to ?? 'test@zintrust.com',
        messageId: result.messageId,
        driver: result.driver,
        ok: result.ok,
    });
}
async function processAdvancedEmailJob(arg) {
    const payload = isJob(arg) ? arg.data : arg;
    const jobId = 'id' in arg && typeof arg.id === 'string'
        ? arg.id
        : `adv-email-${Date.now()}-${generateUuid()}`;
    Logger.info('Processing advanced email job', {
        jobId,
        to: payload.to,
        subject: payload.subject,
        template: payload.template,
    });
    try {
        const { template: selectedTemplate, variables: templateVars } = buildTemplateVariables(payload, jobId);
        // For advanced testing, log deduplication metadata if provided
        if (payload.uniqueId !== undefined) {
            Logger.info('Advanced job uniqueId provided', {
                uniqueId: payload.uniqueId,
                uniqueVia: payload.uniqueVia,
                deduplication: payload.deduplication,
            });
        }
        await sendEmail(payload, selectedTemplate, templateVars);
    }
    catch (error) {
        Logger.error('Advanced email send failed', {
            jobId,
            to: payload.to ?? 'test@zintrust.com',
            template: payload.template ?? 'general',
            error: error,
        });
        throw error;
    }
}
const advancedWorkerOptions = {
    kindLabel: 'Advanced Email Job',
    defaultQueueName: 'advanced-queue',
    maxAttempts: 3,
    getLogFields: (payload) => ({
        to: payload.to,
        subject: payload.subject,
        template: payload.template ?? 'general',
    }),
    handle: processAdvancedEmailJob,
};
export const AdvancEmailWorker = createQueueWorker(advancedWorkerOptions);
export const AdvancEmailQueue = {
    async add(payload, queueName = 'advanced-queue', options) {
        // Merge payload envelope with options for advanced tests
        const queuePayload = {
            ...payload,
            ...options,
            timestamp: Date.now(),
            attempts: 0,
        };
        Logger.info('queuePayload :', queuePayload);
        return Queue.enqueue(queueName, queuePayload);
    },
    async processOne(queueName = 'advanced-queue') {
        return AdvancEmailWorker.processOne(queueName);
    },
    async processAll(queueName = 'advanced-queue') {
        return AdvancEmailWorker.processAll(queueName);
    },
    async start(queueName = 'advanced-queue') {
        void AdvancEmailWorker.startWorker({ queueName });
    },
};
export { AdvancEmailWorker as AdvancEmailWorkerInstance };
export default async function advancedEmailJobProcessor(job) {
    await processAdvancedEmailJob(job);
}
export const ZinTrustProcessor = async (job) => {
    await processAdvancedEmailJob(job);
};
