package org.dromara.talk.controller.api;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.talk.domain.dto.WhatsAppMessageRequest;
import org.dromara.talk.service.IWhatsAppHistoryService;
import org.springframework.web.bind.annotation.*;

/**
 * WhatsApp历史记录API接口(对外)
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/talk/whatsapp/history")
@CrossOrigin(origins = "*", maxAge = 3600)
public class WhatsAppHistoryApiController {

    private final IWhatsAppHistoryService whatsAppHistoryService;

    /**
     * 接收WhatsApp消息
     */
    @PostMapping("/receive")
    public R<Void> receiveMessage(@RequestBody WhatsAppMessageRequest request) {
        try {
            Boolean success = whatsAppHistoryService.receiveMessage(request);
            if (Boolean.TRUE.equals(success)) {
                return R.ok();
            } else {
                return R.fail("保存消息失败");
            }
        } catch (Exception e) {
            log.error("保存WhatsApp消息失败", e);
            return R.fail("保存消息失败: " + e.getMessage());
        }
    }
}
