package com.sonic.pigeon.controller.mock;

import com.netease.nim.server.sdk.im.v2.users.response.GetUserResponseV2;
import com.sonic.common.rpc.Result;
import com.sonic.pigeon.domain.input.FeedbackInput;
import com.sonic.pigeon.lib.input.*;
import com.sonic.pigeon.lib.output.HistoryMessageOutput;
import com.sonic.pigeon.service.*;
import com.sonic.sdk.api.annotation.IgnoreAuth;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Mock测试
 */
@Slf4j
@RestController
public class MockController {

    @Autowired
    private ImMessageService imMessageService;
    @Autowired
    private ImUserService imUserService;
    @Autowired
    private CommonSendMqService commonSendMqService;
    @Autowired
    private ImConversationService imConversationService;
    @Autowired
    private AiChatFeedbackService aiChatFeedbackService;

    @IgnoreAuth
    @ApiOperation(value = "发送Im消息v1", tags = {"Mock-IM消息"})
    @PostMapping("/mock/im-message/send-ai-message-text")
    public Result<Void> sendAiTextMessage(@RequestBody @Valid SendAiTextMessageInput input) {
        imMessageService.sendUserTextMessage(input);
        //发送mq消息
        commonSendMqService.sendAiChatMqV1(input.getFromUserId(), input.getToUserId(), input.getContent());
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "发送Im消息v2", tags = {"Mock-IM消息"})
    @PostMapping("/mock/im-message/send-ai-message-customer")
    public Result<Void> sendAiCustomerMessage(@RequestBody @Valid SendAiTextMessageInput input) {
        imMessageService.sendUserTextMessage(input);
        //发送mq消息
        commonSendMqService.sendAiChatMqV2(input.getFromUserId(), input.getToUserId(), input.getContent());
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "创建Im用户", tags = {"Mock-IM消息"})
    @PostMapping("/mock/im-user/create")
    public Result<GetUserResponseV2> createImUser(@RequestBody @Valid CreateImUserInput input) {
        imUserService.createImUser(input);
        return Result.success(imUserService.getImUser(input.getUserId(), input.getImUserType()));
    }

    @IgnoreAuth
    @ApiOperation(value = "获取Im用户", tags = {"Mock-IM消息"})
    @PostMapping("/mock/im-user/get")
    public Result<GetUserResponseV2> getImUser(@RequestBody @Valid CreateImUserInput input) {
        return Result.success(imUserService.getImUser(input.getUserId(), input.getImUserType()));
    }

    @IgnoreAuth
    @ApiOperation(value = "更新会话扩展字段", tags = {"Mock-会话处理"})
    @PostMapping("/mock/im-conversation/update")
    public Result<Void> updateImConversation(@RequestBody @Valid UpdateConversationInput input) {
        imConversationService.updateConversationExtension(input);
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "会话反馈", tags = {"Mock-会话处理"})
    @PostMapping("/mock/fb/v1")
    public Result<Void> feedback(@RequestBody FeedbackInput input) {
        aiChatFeedbackService.feedback(439058245812225L, input);
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "发送Im自定义消息", tags = {"API-IM消息"})
    @PostMapping("/mock/im-message/send-ai-customer-message")
    public Result<Void> sendAiCustomerMessage(@RequestBody @Valid SendAiCustomerMessageInput input) {
        imMessageService.sendAiToUserCustomerMessage(input);
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "获取历史消息", tags = {"Mock-IM消息"})
    @PostMapping("/mock/im-message/get-history-message")
    public Result<List<HistoryMessageOutput>> getUserToAiHistoryMessage(@RequestBody @Validated HistoryMessageInput input) {
        return Result.success(imMessageService.getUserToAiHistoryMessage(input));
    }

}
