package com.walkguide.controller; import com.walkguide.dto.ApiResponse; import com.walkguide.dto.request.CallNotifyRequest; import com.walkguide.dto.request.CallTokenRequest; import com.walkguide.dto.response.AgoraTokenResponse; import com.walkguide.security.SecurityHelper; import com.walkguide.service.AgoraTokenService; import com.walkguide.service.CallNotificationService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; 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; import java.util.Map; @RestController @RequestMapping("/api/v1/shared/call") @RequiredArgsConstructor @Slf4j @Tag(name = "Call", description = "VoIP call via Agora RTC") @SecurityRequirement(name = "bearerAuth") public class CallController { private final AgoraTokenService agoraTokenService; private final CallNotificationService callNotificationService; @PostMapping("/token") @Operation(summary = "Generate Agora token", description = "Caller requests a token before joining Agora") public ResponseEntity> generateToken( @Valid @RequestBody CallTokenRequest req) { Long callerId = SecurityHelper.getCurrentUserId(); AgoraTokenResponse response = agoraTokenService.generateToken(callerId, req.getReceiverId()); log.info("[CALL] Token generated | caller={} receiver={} channel={}", callerId, req.getReceiverId(), response.getChannelName()); return ResponseEntity.ok(ApiResponse.ok(response, "Token Agora berhasil digenerate")); } @PostMapping("/notify") @Operation(summary = "Notify receiver of incoming call") public ResponseEntity> notifyCall( @Valid @RequestBody CallNotifyRequest req) { Long callerId = SecurityHelper.getCurrentUserId(); String message = callNotificationService.notifyIncomingCall(callerId, req); return ResponseEntity.ok(ApiResponse.ok(null, message)); } @PostMapping("/end") @Operation(summary = "Notify end of call") public ResponseEntity> endCall( @RequestBody Map body) { Long callerId = SecurityHelper.getCurrentUserId(); Long otherId = body.get("otherId"); callNotificationService.notifyCallEnded(callerId, otherId); return ResponseEntity.ok(ApiResponse.ok(null, "Call ended")); } }