package com.ruoyi.novel.utils;

import com.ruoyi.novel.service.IAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

// AdPlatformClient.java
@Component
public class AdPlatformClient {
    @Autowired
    private IAdService adService;

    @Autowired
    private RestTemplate restTemplate;
    @Value("${ad.tencent.url}")
    private String tencentUrl;

    @Value("${ad.pangle.url}")
    private String pangleUrl;
    public void syncAdPlatform(AdLog adLog) {
        Map<String, AdPlatformConfig> configs = adService.loadAdConfigs();
        AdPlatformConfig config = configs.get(adLog.getAdPlatform());

        if (config == null) {
            throw new ServiceException("广告平台配置不存在: " + adLog.getAdPlatform());
        }

        // 构建请求参数
        String payload = buildPayload(adLog, config.getParamsTemplate());

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(config.getAuthToken());

        // 发送请求
        HttpEntity<String> entity = new HttpEntity<>(payload, headers);
        ResponseEntity<String> response = restTemplate.exchange(
                config.getApiUrl(), HttpMethod.POST, entity, String.class);

        // 处理响应
        if (!response.getStatusCode().is2xxSuccessful()) {
            throw new ServiceException("广告平台同步失败: " + response.getBody());
        }

        log.info("广告同步成功: logId={}, platform={}", adLog.getId(), adLog.getAdPlatform());
    }
    private String buildPayload(AdLog adLog, String template) {
        // 使用占位符替换实际值
        return template
                .replace("${userId}", adLog.getUserId().toString())
                .replace("${chapterId}", adLog.getChapterId().toString())
                .replace("${timestamp}", String.valueOf(System.currentTimeMillis()));
    }
    public void syncTencent(AdLog adLog) {
        Map<String, String> params = new HashMap<>();
        params.put("user_id", adLog.getUserId().toString());
        params.put("chapter_id", adLog.getChapterId().toString());
        params.put("event_time", String.valueOf(adLog.getViewTime().getTime()));

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.postForObject(tencentUrl, params, String.class);
    }

    public void syncPangle(AdLog adLog) {
        // 穿山甲平台需要不同的参数格式
        JSONObject payload = new JSONObject();
        payload.put("user", adLog.getUserId());
        payload.put("content_id", adLog.getChapterId());
        payload.put("timestamp", System.currentTimeMillis());

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> entity = new HttpEntity<>(payload.toString(), headers);
        restTemplate.postForObject(pangleUrl, entity, String.class);
    }
}