package kz.konturai.parser.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mail.javamail.JavaMailSender;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;

import static org.mockito.ArgumentMatchers.any;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class EmailServiceTest {

    @Mock
    private JavaMailSender mailSender;

    @Mock
    private MimeMessage mimeMessage;

    private EmailService emailService;

    @BeforeEach
    void setUp() {
        emailService = new EmailService(mailSender);
    }

    @Test
    void testSendReportByEmail() throws MessagingException {
        // Arrange
        String recipientEmail = "test@example.com";
        String reportTitle = "Test Report";
        String authorName = "Test Author";
        String companyName = "Test Company";
        byte[] pdfBytes = "test pdf content".getBytes();
        String filename = "test-report.pdf";

        when(mailSender.createMimeMessage()).thenReturn(mimeMessage);

        // Act
        emailService.sendReportByEmail(recipientEmail, reportTitle, authorName, companyName, pdfBytes, filename);

        // Assert
        verify(mailSender, times(1)).createMimeMessage();
        verify(mailSender, times(1)).send(any(MimeMessage.class));
    }

    @Test
    void testSendReportByEmailWithNullValues() throws MessagingException {
        // Arrange
        String recipientEmail = "test@example.com";
        String reportTitle = null;
        String authorName = null;
        String companyName = null;
        byte[] pdfBytes = "test pdf content".getBytes();
        String filename = "test-report.pdf";

        when(mailSender.createMimeMessage()).thenReturn(mimeMessage);

        // Act
        emailService.sendReportByEmail(recipientEmail, reportTitle, authorName, companyName, pdfBytes, filename);

        // Assert
        verify(mailSender, times(1)).createMimeMessage();
        verify(mailSender, times(1)).send(any(MimeMessage.class));
    }

    @Test
    void testSendReportByEmailWithMessagingException() throws MessagingException {
        // Arrange
        String recipientEmail = "test@example.com";
        String reportTitle = "Test Report";
        String authorName = "Test Author";
        String companyName = "Test Company";
        byte[] pdfBytes = "test pdf content".getBytes();
        String filename = "test-report.pdf";

        when(mailSender.createMimeMessage()).thenThrow(new RuntimeException("SMTP error"));

        // Act
        assertThrows(RuntimeException.class, () -> emailService.sendReportByEmail(
                recipientEmail, reportTitle, authorName, companyName, pdfBytes, filename));

        // Assert
        verify(mailSender, times(1)).createMimeMessage();
        verify(mailSender, never()).send(any(MimeMessage.class));
    }
}
