package com.qqchen.deploy.backend.notification;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qqchen.deploy.backend.notification.dto.NotificationChannelDTO;
import com.qqchen.deploy.backend.notification.entity.config.EmailNotificationConfig;
import com.qqchen.deploy.backend.notification.entity.config.WeworkNotificationConfig;
import com.qqchen.deploy.backend.notification.enums.NotificationChannelTypeEnum;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 通知配置 JSON 序列化/反序列化测试
 * 验证重构后的 BaseNotificationConfig 多态机制是否正常
 */
@SpringBootTest
public class NotificationConfigJsonTest {

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void testWeworkConfigSerialization() throws Exception {
        NotificationChannelDTO dto = new NotificationChannelDTO();
        dto.setName("测试企微渠道");
        dto.setChannelType(NotificationChannelTypeEnum.WEWORK);
        dto.setEnabled(true);
        
        WeworkNotificationConfig config = new WeworkNotificationConfig();
        config.setKey("test-webhook-key-12345");
        dto.setConfig(config);

        String json = objectMapper.writeValueAsString(dto);
        System.out.println("Wework Serialization: " + json);
        
        assertTrue(json.contains("\"channelType\":\"WEWORK\""));
        assertTrue(json.contains("\"key\":\"test-webhook-key-12345\""));

        NotificationChannelDTO deserialized = objectMapper.readValue(json, NotificationChannelDTO.class);
        
        assertNotNull(deserialized.getConfig());
        assertTrue(deserialized.getConfig() instanceof WeworkNotificationConfig);
        assertEquals("test-webhook-key-12345", ((WeworkNotificationConfig) deserialized.getConfig()).getKey());
    }

    @Test
    public void testEmailConfigSerialization() throws Exception {
        NotificationChannelDTO dto = new NotificationChannelDTO();
        dto.setName("测试邮件渠道");
        dto.setChannelType(NotificationChannelTypeEnum.EMAIL);
        dto.setEnabled(true);
        
        EmailNotificationConfig config = new EmailNotificationConfig();
        config.setSmtpHost("smtp.example.com");
        config.setSmtpPort(465);
        config.setUsername("noreply@example.com");
        config.setPassword("password123");
        config.setFrom("noreply@example.com");
        config.setFromName("Deploy Platform");
        config.setUseSsl(true);
        dto.setConfig(config);

        String json = objectMapper.writeValueAsString(dto);
        System.out.println("Email Serialization: " + json);
        
        assertTrue(json.contains("\"channelType\":\"EMAIL\""));
        assertTrue(json.contains("\"smtpHost\":\"smtp.example.com\""));

        NotificationChannelDTO deserialized = objectMapper.readValue(json, NotificationChannelDTO.class);
        
        assertNotNull(deserialized.getConfig());
        assertTrue(deserialized.getConfig() instanceof EmailNotificationConfig);
        EmailNotificationConfig deserializedConfig = (EmailNotificationConfig) deserialized.getConfig();
        assertEquals("smtp.example.com", deserializedConfig.getSmtpHost());
        assertEquals(465, deserializedConfig.getSmtpPort());
        assertEquals("noreply@example.com", deserializedConfig.getUsername());
    }
}
