import type { IExecuteFunctions, IBinaryData } from 'n8n-workflow';

import * as sendOperation from '../../v2/send.operation';
import { prepareBinariesDataList } from '../../../../utils/binary';
import type { Mock, Mocked } from 'vitest';
import type * as _importType0 from '../../v2/utils';

const transporter = { sendMail: vi.fn() };

vi.mock('../../v2/utils', async () => {
	const originalModule = await vi.importActual<typeof _importType0>('../../v2/utils');
	return {
		...originalModule,
		configureTransport: vi.fn(() => transporter),
	};
});

describe('Test EmailSendV2, send operation', () => {
	let mockExecuteFunctions: Mocked<IExecuteFunctions>;

	beforeEach(() => {
		mockExecuteFunctions = {
			getInputData: vi.fn(),
			getNode: vi.fn(),
			getInstanceId: vi.fn(),
			getCredentials: vi.fn(),
			getNodeParameter: vi.fn(),
			helpers: {
				assertBinaryData: vi.fn(),
				getBinaryDataBuffer: vi.fn(),
			},
		} as unknown as Mocked<IExecuteFunctions>;
		vi.clearAllMocks();
	});

	describe('comma-separated attachment strings', () => {
		it('should process comma-separated attachment names with spaces', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
						file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
						file3: { data: 'data3', mimeType: 'text/plain', fileName: 'file3.txt' } as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: 'file1, file2, file3', appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: [
						expect.objectContaining({ filename: 'file1.txt', cid: 'file1' }),
						expect.objectContaining({ filename: 'file2.txt', cid: 'file2' }),
						expect.objectContaining({ filename: 'file3.txt', cid: 'file3' }),
					],
				}),
			);
		});

		it('should process comma-separated attachment names without spaces', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
						file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: 'file1,file2', appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: [
						expect.objectContaining({ filename: 'file1.txt', cid: 'file1' }),
						expect.objectContaining({ filename: 'file2.txt', cid: 'file2' }),
					],
				}),
			);
		});

		it('should trim extra spaces from attachment names', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
						file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: '  file1  ,  file2  ', appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: [
						expect.objectContaining({ filename: 'file1.txt', cid: 'file1' }),
						expect.objectContaining({ filename: 'file2.txt', cid: 'file2' }),
					],
				}),
			);
		});

		it('should process single attachment name', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						singleFile: {
							data: 'data1',
							mimeType: 'text/plain',
							fileName: 'single.txt',
						} as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: 'singleFile', appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: [expect.objectContaining({ filename: 'single.txt', cid: 'singleFile' })],
				}),
			);
		});
	});

	describe('prepareBinariesDataList helper function', () => {
		it('should process string attachment names correctly', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						file1: { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' } as IBinaryData,
						file2: { data: 'data2', mimeType: 'text/plain', fileName: 'file2.txt' } as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: 'file1, file2', appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			const result = prepareBinariesDataList('file1, file2');
			expect(result).toEqual(['file1', 'file2']);

			await sendOperation.execute.call(mockExecuteFunctions);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: expect.arrayContaining([
						expect.objectContaining({ cid: 'file1' }),
						expect.objectContaining({ cid: 'file2' }),
					]),
				}),
			);
		});

		it('should wrap IBinaryData object in array', () => {
			const input = { data: 'data1', mimeType: 'text/plain', fileName: 'file.txt' };
			const result = prepareBinariesDataList(input);
			expect(result).toEqual([input]);
			expect(result).toHaveLength(1);
		});

		it('should return IBinaryData array unchanged', () => {
			const input = [
				{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
				{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
			];
			const result = prepareBinariesDataList(input);
			expect(result).toEqual(input);
			expect(result).toHaveLength(2);
		});

		it('should return string array unchanged', () => {
			const input = ['file1', 'file2', 'file3'];
			const result = prepareBinariesDataList(input);
			expect(result).toEqual(['file1', 'file2', 'file3']);
		});

		it('should process IBinaryData object as attachment', async () => {
			const binaryDataObject = { data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' };
			const items = [
				{
					json: { data: 'test' },
					binary: {} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: binaryDataObject, appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string | IBinaryData) => {
					if (typeof propertyName === 'object') {
						return propertyName;
					}
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string | IBinaryData) => {
					const binaryData =
						typeof propertyName === 'object'
							? propertyName
							: items[itemIndex].binary![propertyName];
					return Buffer.from(binaryData.data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			const result = prepareBinariesDataList(binaryDataObject);
			expect(result).toEqual([binaryDataObject]);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: [expect.objectContaining({ filename: 'file1.txt', cid: binaryDataObject })],
				}),
			);
		});

		it('should process IBinaryData array as attachments', async () => {
			const binaryDataArray = [
				{ data: 'data1', mimeType: 'text/plain', fileName: 'file1.txt' },
				{ data: 'data2', mimeType: 'image/png', fileName: 'file2.png' },
			];
			const items = [
				{
					json: { data: 'test' },
					binary: {} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ attachments: binaryDataArray, appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string | IBinaryData) => {
					if (typeof propertyName === 'object') {
						return propertyName;
					}
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string | IBinaryData) => {
					const binaryData =
						typeof propertyName === 'object'
							? propertyName
							: items[itemIndex].binary![propertyName];
					return Buffer.from(binaryData.data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			const result = prepareBinariesDataList(binaryDataArray);
			expect(result).toEqual(binaryDataArray);
			expect(result).toHaveLength(2);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.objectContaining({
					attachments: [
						expect.objectContaining({ filename: 'file1.txt' }),
						expect.objectContaining({ filename: 'file2.png' }),
					],
				}),
			);
		});
	});

	describe('file attachments (non-inline)', () => {
		it('should attach file attachments without cid', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						file1: {
							data: 'data1',
							mimeType: 'application/pdf',
							fileName: 'doc.pdf',
						} as IBinaryData,
						file2: { data: 'data2', mimeType: 'text/csv', fileName: 'data.csv' } as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ fileAttachments: 'file1, file2', appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			const callArg = transporter.sendMail.mock.calls[0][0];
			expect(callArg.attachments).toEqual([
				expect.objectContaining({ filename: 'doc.pdf' }),
				expect.objectContaining({ filename: 'data.csv' }),
			]);
			expect(callArg.attachments[0]).not.toHaveProperty('cid');
			expect(callArg.attachments[1]).not.toHaveProperty('cid');
		});

		it('should combine inline and file attachments', async () => {
			const items = [
				{
					json: { data: 'test' },
					binary: {
						logo: { data: 'data1', mimeType: 'image/png', fileName: 'logo.png' } as IBinaryData,
						report: {
							data: 'data2',
							mimeType: 'application/pdf',
							fileName: 'report.pdf',
						} as IBinaryData,
					} as Record<string, IBinaryData>,
				},
			];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({
					attachments: 'logo',
					fileAttachments: 'report',
					appendAttribution: false,
				})
				.mockReturnValueOnce('<p><img src="cid:logo"></p>');

			(mockExecuteFunctions.helpers.assertBinaryData as Mock).mockImplementation(
				(itemIndex: number, propertyName: string) => {
					return items[itemIndex].binary![propertyName];
				},
			);

			(mockExecuteFunctions.helpers.getBinaryDataBuffer as Mock).mockImplementation(
				async (itemIndex: number, propertyName: string) => {
					return Buffer.from(items[itemIndex].binary![propertyName].data);
				},
			);

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			const callArg = transporter.sendMail.mock.calls[0][0];
			expect(callArg.attachments).toHaveLength(2);
			expect(callArg.attachments[0]).toEqual(
				expect.objectContaining({ filename: 'logo.png', cid: 'logo' }),
			);
			expect(callArg.attachments[1]).toEqual(expect.objectContaining({ filename: 'report.pdf' }));
			expect(callArg.attachments[1]).not.toHaveProperty('cid');
		});
	});

	describe('emails without attachments', () => {
		it('should send email when no attachments specified', async () => {
			const items = [{ json: { data: 'test' } }];

			mockExecuteFunctions.getInputData.mockReturnValue(items);
			mockExecuteFunctions.getNode.mockReturnValue({ typeVersion: 2.0 } as any);
			mockExecuteFunctions.getInstanceId.mockReturnValue('instanceId');
			mockExecuteFunctions.getCredentials.mockResolvedValue({
				host: 'smtp.example.com',
				port: 587,
			});

			mockExecuteFunctions.getNodeParameter
				.mockReturnValueOnce('from@example.com')
				.mockReturnValueOnce('to@example.com')
				.mockReturnValueOnce('Test Subject')
				.mockReturnValueOnce('html')
				.mockReturnValueOnce({ appendAttribution: false })
				.mockReturnValueOnce('<p>Test HTML</p>');

			transporter.sendMail.mockResolvedValue({ messageId: 'test-id' });

			await sendOperation.execute.call(mockExecuteFunctions);

			expect(transporter.sendMail).toHaveBeenCalledWith(
				expect.not.objectContaining({
					attachments: expect.anything(),
				}),
			);
		});
	});
});
