/* istanbul ignore file */
import AdvancedEmailJobService from '../app/Jobs/AdvancedEmailJobService.js';
import { Mail } from '../src/tools/mail/Mail.js';
import { Logger, Router } from '../src/index.js';
// Ensure TestWorker is registered for dashboard visibility
import '../app/Workers/TestWorker.js';
export const demonstrateDelayScheduling = async () => {
    // Schedule email for 1 hour from now - job won't process until delay expires
    await AdvancedEmailJobService.sendScheduledLock('scheduled@zintrust.com', 'Meeting Reminder', 'welcome', { meetingTime: '2026-02-01T10:00:00Z' }, 3600000 // 1 hour delay in milliseconds
    );
    // AdvancedEmailJobService.processAll();
};
export const demonstratePriorityJobOrdering = async () => {
    // High priority urgent email - processes before lower priority jobs
    await AdvancedEmailJobService.sendHighPriority('urgent@zintrust.com', 'URGENT: System Alert', 'welcome', { alertType: 'critical' }, { priority: 10, attempts: 5 }, // Higher number = higher priority,
    'advanced-queue-redis');
};
export const demonstrateRetryControl = async () => {
    // Email with custom retry attempts and exponential backoff
    await AdvancedEmailJobService.sendHighPriority('retry@zintrust.com', 'Retry Test', 'welcome', { test: 'retry-logic' }, { attempts: 7, delay: 0 } // Will retry 7 times with exponential backoff
    );
};
export const demonstrateDeduplication = async () => {
    // Prevents duplicate emails from being sent within 24-hour window
    await AdvancedEmailJobService.sendWithDeduplication('dedup@zintrust.com', 'Welcome Email', 'welcome', { name: 'User' }, 'welcome-user-123', // Same uniqueId = same job
    'example-redis1');
    await AdvancedEmailJobService.sendWithDeduplication('dedup-2@zintrust.com', 'Weekly Digest', 'welcome', { digest: 'week-1', delay: 300000 }, 'weekly-digest-001', 'example-redis1');
    await AdvancedEmailJobService.sendWithDeduplication('dedup-3@zintrust.com', 'Invoice Ready', 'welcome', { invoiceId: 'inv-8899', delay: 300000 }, 'invoice-inv-8899', 'advanced-queue');
};
export const demonstrateCustomLockProvider = async () => {
    // Use custom lock provider (redis/database/memory) for uniqueness
    await AdvancedEmailJobService.sendWithUniqueLock('unique@zintrust.com', 'Password Reset', 'welcome', { token: 'abc123' }, 'redis', // uniqueVia: 'redis' | 'database' | 'memory'
    'example-redis1');
    await AdvancedEmailJobService.sendWithUniqueLock('unique-2@zintrust.com', 'Email Verification', 'welcome', { token: 'verify-456' }, 'memory', 'advanced-queue');
    await AdvancedEmailJobService.sendWithUniqueLock('unique-3@zintrust.com', 'Account Recovery', 'welcome', { token: 'recover-789' }, 'database', 'example-redis1');
};
export const demonstrateBulkProcessing = async () => {
    // Send bulk emails with batch ID and per-recipient deduplication
    await AdvancedEmailJobService.sendBulk(['bulk1@zintrust.com', 'bulk2@zintrust.com', 'bulk3@zintrust.com'], 'Monthly Newsletter', 'welcome', { campaign: 'Q1-2026', issue: 'January' }, 'batch-newsletter-202601', // batchId for tracking
    'advanced-queue');
};
export const demonstrateMetadataTracking = async () => {
    // Email with campaign tracking metadata
    await AdvancedEmailJobService.sendWithMetadata('metadata@zintrust.com', 'Campaign Email', 'welcome', { userName: 'Alice' }, {
        campaign: 'winter-sale',
        source: 'web-signup',
        priority: 'high',
        tags: ['marketing', 'promotion', 'new-user'],
    }, 'example-redis1');
};
export const demonstrateCombinedAdvancedOptions = async () => {
    // High-priority scheduled email with retry logic
    await AdvancedEmailJobService.sendHighPriority('combined@zintrust.com', 'VIP Scheduled Notification', 'welcome', { vipLevel: 'platinum' }, {
        priority: 10, // High priority
        delay: 300000, // 5 minutes delay
        attempts: 5, // Retry up to 5 times
    }, 'example-redis1');
};
const TestAdvancedJob = async () => {
    // ====================================================================
    // SCENARIO 1: Delay Scheduling (delay option)
    // ====================================================================
    // await demonstrateDelayScheduling();
    // ====================================================================
    // SCENARIO 2: Priority Job Ordering (priority option)
    // ====================================================================
    await demonstratePriorityJobOrdering();
    // ====================================================================
    // SCENARIO 3: Retry Control (attempts + backoff options)
    // ====================================================================
    // await demonstrateRetryControl();
    // ====================================================================
    // SCENARIO 4: Deduplication Support (uniqueId + deduplication options)
    // ====================================================================
    // await demonstrateDeduplication();
    // ====================================================================
    // SCENARIO 5: Custom Lock Provider (uniqueVia option)
    // ====================================================================
    // await demonstrateCustomLockProvider();
    // ====================================================================
    // SCENARIO 6: Bulk Processing with Deduplication
    // ====================================================================
    // await demonstrateBulkProcessing();
    // ====================================================================
    // SCENARIO 7: Metadata Tracking (custom envelope data)
    // ====================================================================
    // await demonstrateMetadataTracking();
    // ====================================================================
    // SCENARIO 8: Combined Advanced Options
    // ====================================================================
    // await demonstrateCombinedAdvancedOptions();
};
export const registerMailUiPag = async (router) => {
    /* istanbul ignore next */
    // const { EmailJobService } = await import('../app/Jobs/EmailJobService');
    const handler = async (req, res) => {
        try {
            await TestAdvancedJob();
        }
        catch (error) {
            Logger.warn('Mail preview queue trigger failed', {
                route: '/mail/:template',
                template: req.getParam('template') ?? 'welcome',
                error: error instanceof Error ? error.message : String(error),
            });
        }
        const templateName = req.getParam('template') ?? 'welcome';
        const templates = {
            welcome: 'welcome',
            general: 'general',
            custom: 'custom',
            'job-completed': 'job-completed',
            'performance-report': 'performance-report',
            'password-reset': 'password-reset',
            'worker-alert': 'worker-alert',
        };
        const finalTemplate = templates[templateName] ?? 'welcome';
        const variablesByTemplate = {
            welcome: { name: 'Alice' },
            general: {
                name: 'Alice',
                headline: 'Hello Alice',
                message: 'Welcome to ZinTrust platform.',
                primary_color: '#0ea5e9',
            },
            custom: { name: 'Alice' },
        };
        const html = await Mail.render({
            template: finalTemplate,
            variables: variablesByTemplate[finalTemplate] ?? { name: 'Alice' },
        });
        res.html(html);
    };
    if (typeof Router?.get !== 'function') {
        return;
    }
    Router.get(router, '/mail/:template', handler);
};
