/**
 * Tests for Enhanced Email Generator
 */

import { EnhancedEmailGenerator } from './enhanced-email-generator';
import { GeneratorContext } from '../types';
import { Workflow } from '../../types/workflow';

describe('EnhancedEmailGenerator', () => {
  let generator: EnhancedEmailGenerator;

  beforeEach(() => {
    generator = new EnhancedEmailGenerator();
  });

  describe('Configuration Generation', () => {
    it('should generate email configuration', () => {
      const workflow: Workflow = {
        workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
        stack: {},
        email: {
          provider: 'sendgrid',
          config: {
            from_email: 'noreply@example.com',
            from_name: 'Example App',
          },
        },
      };

      const context: GeneratorContext = { workflow, outputDirectory: '/tmp/test',
        executionPlan: { workflow, componentPlans: new Map() },
      };

      const result = generator.generate(context);

      expect(result.files.length).toBeGreaterThan(0);
      const configFile = result.files.find(f => f.path.includes('config/email.ts'));
      expect(configFile).toBeDefined();
      expect(configFile!.content).toContain('sendgrid');
    });
  });

  describe('Email Service', () => {
    it('should generate email service', () => {
      const workflow: Workflow = {
        workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
        stack: {},
        email: {
          provider: 'sendgrid',
        },
      };

      const context: GeneratorContext = { workflow, outputDirectory: '/tmp/test',
        executionPlan: { workflow, componentPlans: new Map() },
      };

      const result = generator.generate(context);

      const serviceFile = result.files.find(f => f.path.includes('email-service.ts'));
      expect(serviceFile).toBeDefined();
      expect(serviceFile!.content).toContain('EmailService');
      expect(serviceFile!.content).toContain('send');
    });
  });

  describe('Email Providers', () => {
    const providers = ['sendgrid', 'postmark', 'resend', 'ses'];

    providers.forEach(provider => {
      it(`should generate ${provider} provider`, () => {
        const workflow: Workflow = {
          workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
          stack: {},
          email: {
            provider: provider as any,
          },
        };

        const context: GeneratorContext = { workflow, outputDirectory: '/tmp/test',
          executionPlan: { workflow, componentPlans: new Map() },
        };

        const result = generator.generate(context);

        const providerFile = result.files.find(f => f.path.includes('email-provider.ts'));
        expect(providerFile).toBeDefined();
        expect(providerFile!.content).toContain('emailProvider');
      });
    });
  });

  describe('Email Templates', () => {
    it('should generate email templates', () => {
      const workflow: Workflow = {
        workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
        stack: {},
        email: {
          provider: 'sendgrid',
          templates: [
            {
              name: 'welcome',
              subject: 'Welcome!',
              variables: ['name', 'email'],
            },
          ],
        },
      };

      const context: GeneratorContext = { workflow, outputDirectory: '/tmp/test',
        executionPlan: { workflow, componentPlans: new Map() },
      };

      const result = generator.generate(context);

      const templateFile = result.files.find(f => f.path.includes('welcome.html'));
      expect(templateFile).toBeDefined();
      expect(templateFile!.content).toContain('Welcome!');
    });
  });

  describe('Email Queue', () => {
    it('should generate email queue when enabled', () => {
      const workflow: Workflow = {
        workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
        stack: {},
        email: {
          provider: 'sendgrid',
          queue: {
            enabled: true,
            redis_url: 'redis://localhost:6379',
          },
        },
      };

      const context: GeneratorContext = { workflow, outputDirectory: '/tmp/test',
        executionPlan: { workflow, componentPlans: new Map() },
      };

      const result = generator.generate(context);

      const queueFile = result.files.find(f => f.path.includes('email-queue.ts'));
      expect(queueFile).toBeDefined();
      expect(queueFile!.content).toContain('bullmq');

      const workerFile = result.files.find(f => f.path.includes('email-worker.ts'));
      expect(workerFile).toBeDefined();
      expect(workerFile!.content).toContain('Worker');
    });
  });

  describe('Edge Cases', () => {
    it('should return empty array when email not configured', () => {
      const workflow: Workflow = {
        workflow: { id: 'test', version: '1.0', name: 'Test', description: 'Test workflow' },
        stack: {},
      };

      const context: GeneratorContext = { workflow, outputDirectory: '/tmp/test',
        executionPlan: { workflow, componentPlans: new Map() },
      };

      const result = generator.generate(context);

      expect(result.files).toEqual([]);
    });
  });
});
