package com.qqchen.deploy.backend.workflow.delegate;

import com.qqchen.deploy.backend.workflow.dto.inputmapping.NotificationInputMapping;
import com.qqchen.deploy.backend.workflow.dto.outputs.NotificationOutputs;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 通知任务委派
 * 
 * @author qqchen
 * @since 2025-10-22
 */
@Slf4j
@Component("notificationDelegate")
public class NotificationNodeDelegate extends BaseNodeDelegate<NotificationInputMapping, NotificationOutputs> {

    private final RestTemplate restTemplate = new RestTemplate();

    // TODO: 从数据库中读取webhook配置
    private final String WX_HOOK_API = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=614b110b-8957-4be8-95b9-4eca84c15028";

    @Override
    protected NotificationOutputs executeInternal(
            DelegateExecution execution,
            Map<String, Object> configs,
            NotificationInputMapping input
    ) {
        log.info("Sending notification - channel: {}, title: {}, content: {}", 
            input.getNotificationChannel(), input.getTitle(), input.getContent());

        // 1. 根据notificationChannel获取webhook配置
        // TODO: 从数据库查询通知渠道配置
        
        // 2. 发送通知
        try {
            sendWeChatNotification(input.getTitle(), input.getContent());
            
            // 3. 返回成功结果
            NotificationOutputs outputs = new NotificationOutputs();
            outputs.setStatus(com.qqchen.deploy.backend.workflow.enums.NodeExecutionStatusEnum.SUCCESS);
            outputs.setMessage("通知发送成功");
            return outputs;
            
        } catch (Exception e) {
            log.error("Failed to send notification", e);
            NotificationOutputs outputs = new NotificationOutputs();
            outputs.setStatus(com.qqchen.deploy.backend.workflow.enums.NodeExecutionStatusEnum.FAILURE);
            outputs.setMessage("通知发送失败: " + e.getMessage());
            return outputs;
        }
    }

    /**
     * 发送企业微信通知
     */
    private void sendWeChatNotification(String title, String content) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String message = String.format("%s\n%s", title, content);
        String body = String.format(
            "{\n" +
            "  \"msgtype\": \"text\",\n" +
            "  \"text\": {\n" +
            "    \"content\": \"%s\"\n" +
            "  }\n" +
            "}", message
        );

        HttpEntity<String> entity = new HttpEntity<>(body, headers);

        restTemplate.exchange(
            WX_HOOK_API,
            HttpMethod.POST,
            entity,
            String.class
        );
        
        log.info("WeChat notification sent successfully");
    }
}
