package com.sonic.cow.controller.web;

import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.cow.domain.input.GenerateRtcTokenInput;
import com.sonic.cow.domain.input.VoiceChatOptInput;
import com.sonic.cow.domain.output.GenerateRtcTokenOutput;
import com.sonic.cow.limit.RequestLimit;
import com.sonic.cow.service.VoiceChatService;
import com.sonic.sdk.api.annotation.IgnoreAuth;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Web-AI语音通话
 *
 * @author: mzc
 * @date: 2025-07-30 11:43
 **/
@RestController
@Slf4j
public class VoiceChatWeb {

    @Autowired
    private VoiceChatService voiceChatService;


    @RequestLimit(count = 100, time = 86400000)
    @ApiOperation(value = "获取RTC token", tags = {"Web-AI语音通话"})
    @PostMapping(value = "/web/voice-chat/gen-rtc-tk")
    public Result<GenerateRtcTokenOutput> genRTCTk(@RequestBody GenerateRtcTokenInput input, @ApiParam(hidden = true) Session session, @ApiParam(hidden = true) HttpServletRequest request) {
        return Result.success(voiceChatService.generateRtcToken(session.getUserId(), input));
    }

    @RequestLimit(count = 100, time = 86400000)
    @ApiOperation(value = "开始，打断，结束语音通话", tags = {"Web-AI语音通话"})
    @PostMapping(value = "/web/voice-chat/opt")
    public Result voiceChatOpt(@RequestBody VoiceChatOptInput input, @ApiParam(hidden = true) Session session, @ApiParam(hidden = true) HttpServletRequest request) {
        input.setEndpoint(session.getEndpoint());
        return Result.success(voiceChatService.voiceChatOpt(session.getUserId(), input));
    }

    @IgnoreAuth
    @ApiOperation(value = "RTC webhook回调通知", tags = {"Web-AI语音通话"})
    @PostMapping(value = "/web/voice-chat/webhook")
    public Result<GenerateRtcTokenOutput> webhook(@ApiParam(hidden = true) HttpServletRequest request, @ApiParam(hidden = true) HttpServletResponse response) {
        voiceChatService.webhook(request, response);
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "会话状态回调通知(监听智能体状态)", tags = {"Web-AI语音通话"})
    @PostMapping(value = "/web/voice-chat/conversation-state-callback")
    public Result<Void> conversationStateCallback(@ApiParam(hidden = true) HttpServletRequest request, @ApiParam(hidden = true) HttpServletResponse response) {
        voiceChatService.conversationStateCallback(request, response);
        return Result.success();
    }

    @IgnoreAuth
    @ApiOperation(value = "实时字幕回调", tags = {"Web-AI语音通话"})
    @PostMapping(value = "/web/voice-chat/rts-callback")
    public Result<Void> rtsCallback(@ApiParam(hidden = true) HttpServletRequest request, @ApiParam(hidden = true) HttpServletResponse response) {
        voiceChatService.rtsCallback(request, response);
        return Result.success();
    }
}
