package com.sikey.wa04.api.websocket.client.rest;

import com.sikey.wa04.business.application.service.message.MessagingSubscribeService;
import com.sikey.wa04.business.infrastructure.configuration.ProjectContextConstant;
import com.sikey.wa04.business.infrastructure.dto.request.publish.PublishSubscribeRequest;
import com.sikey.wa04.business.infrastructure.dto.request.publish.PublishUnsubscribeRequest;
import com.sikey.wa04.common.BaseController;
import com.sikey.wa04.common.interceptor.Authenticated;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 推送订阅接口
 * <br/>
 * 当用户离线的时候会推送离线消息给订阅用户
 *
 * @author luoyangwei
 * @date 2024年10月15日15:09:12
 */
@RestController
@RequestMapping(ProjectContextConstant.WEBSOCKET_URL + "/publish")
public class PublishSubscribeController extends BaseController {

    @Resource
    private MessagingSubscribeService messagingSubscribeService;

    /**
     * 订阅
     *
     * @param request 订阅请求
     * @return 订阅结果
     */
    @Authenticated
    @PostMapping("/subscribe")
    public void subscribe(@Validated @RequestBody PublishSubscribeRequest request) throws Exception {
        messagingSubscribeService.subscribe(request, genericManufacturerHeader());
    }

    /**
     * 取消订阅
     *
     * @param request 取消订阅请求
     * @return 取消订阅结果
     */
    @Authenticated
    @PostMapping("/unsubscribe")
    public void unsubscribe(@Validated @RequestBody PublishUnsubscribeRequest request) {
        messagingSubscribeService.unsubscribe(request);
    }

}
