package com.unicorn.hgzero.stt.controller;

import com.unicorn.hgzero.common.response.ApiResponse;
import com.unicorn.hgzero.stt.dto.RecordingDto;
import com.unicorn.hgzero.stt.service.RecordingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 녹음 관리 컨트롤러
 * 음성 녹음 준비, 시작, 중지, 조회 기능을 제공
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/stt/recordings")
@RequiredArgsConstructor
@Validated
@Tag(name = "녹음 관리", description = "음성 녹음 관리 API")
public class RecordingController {
    
    private final RecordingService recordingService;
    
    @Operation(
        summary = "녹음 준비",
        description = "회의 녹음을 위한 초기화 및 설정을 수행합니다."
    )
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "200", 
            description = "녹음 준비 성공",
            content = @Content(schema = @Schema(implementation = RecordingDto.PrepareResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "400", 
            description = "잘못된 요청"
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "409", 
            description = "이미 진행 중인 녹음 세션 존재"
        )
    })
    @PostMapping("/prepare")
    public ResponseEntity<ApiResponse<RecordingDto.PrepareResponse>> prepareRecording(
            @Valid @RequestBody RecordingDto.PrepareRequest request) {
        log.info("녹음 준비 요청 - meetingId: {}, sessionId: {}", 
                request.getMeetingId(), request.getSessionId());
        
        RecordingDto.PrepareResponse response = recordingService.prepareRecording(request);
        
        return ResponseEntity.ok(ApiResponse.success(response));
    }
    
    @Operation(
        summary = "녹음 시작",
        description = "준비된 녹음 세션을 시작합니다."
    )
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "200", 
            description = "녹음 시작 성공",
            content = @Content(schema = @Schema(implementation = RecordingDto.StatusResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "400", 
            description = "녹음을 시작할 수 없는 상태"
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "녹음 세션을 찾을 수 없음"
        )
    })
    @PostMapping("/{recordingId}/start")
    public ResponseEntity<ApiResponse<RecordingDto.StatusResponse>> startRecording(
            @Parameter(description = "녹음 ID", required = true)
            @PathVariable String recordingId,
            @Valid @RequestBody RecordingDto.StartRequest request) {
        log.info("녹음 시작 요청 - recordingId: {}, startedBy: {}", 
                recordingId, request.getStartedBy());
        
        RecordingDto.StatusResponse response = recordingService.startRecording(recordingId, request);
        
        return ResponseEntity.ok(ApiResponse.success(response));
    }
    
    @Operation(
        summary = "녹음 중지",
        description = "진행 중인 녹음을 중지합니다."
    )
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "200", 
            description = "녹음 중지 성공",
            content = @Content(schema = @Schema(implementation = RecordingDto.StatusResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "400", 
            description = "진행 중인 녹음이 아님"
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "녹음 세션을 찾을 수 없음"
        )
    })
    @PostMapping("/{recordingId}/stop")
    public ResponseEntity<ApiResponse<RecordingDto.StatusResponse>> stopRecording(
            @Parameter(description = "녹음 ID", required = true)
            @PathVariable String recordingId,
            @Valid @RequestBody RecordingDto.StopRequest request) {
        log.info("녹음 중지 요청 - recordingId: {}, stoppedBy: {}", 
                recordingId, request.getStoppedBy());
        
        RecordingDto.StatusResponse response = recordingService.stopRecording(recordingId, request);
        
        return ResponseEntity.ok(ApiResponse.success(response));
    }
    
    @Operation(
        summary = "녹음 상세 조회",
        description = "녹음 세션의 상세 정보를 조회합니다."
    )
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "200", 
            description = "조회 성공",
            content = @Content(schema = @Schema(implementation = RecordingDto.DetailResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "녹음 세션을 찾을 수 없음"
        )
    })
    @GetMapping("/{recordingId}")
    public ResponseEntity<ApiResponse<RecordingDto.DetailResponse>> getRecording(
            @Parameter(description = "녹음 ID", required = true)
            @PathVariable String recordingId) {
        log.debug("녹음 조회 요청 - recordingId: {}", recordingId);
        
        RecordingDto.DetailResponse response = recordingService.getRecording(recordingId);
        
        return ResponseEntity.ok(ApiResponse.success(response));
    }
}