package com.unicorn.hgzero.stt.controller;

import com.unicorn.hgzero.common.response.ApiResponse;
import com.unicorn.hgzero.stt.dto.SpeakerDto;
import com.unicorn.hgzero.stt.service.SpeakerService;
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/speakers")
@RequiredArgsConstructor
@Validated
@Tag(name = "화자 관리", description = "화자 식별 및 관리 API")
public class SpeakerController {
    
    private final SpeakerService speakerService;
    
    @Operation(
        summary = "화자 식별",
        description = "음성 데이터로부터 화자를 식별합니다."
    )
    @ApiResponses(value = {
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "200", 
            description = "화자 식별 성공",
            content = @Content(schema = @Schema(implementation = SpeakerDto.IdentificationResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "400", 
            description = "잘못된 요청"
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "녹음을 찾을 수 없음"
        )
    })
    @PostMapping("/identify")
    public ResponseEntity<ApiResponse<SpeakerDto.IdentificationResponse>> identifySpeaker(
            @Valid @RequestBody SpeakerDto.IdentifyRequest request) {
        log.info("화자 식별 요청 - recordingId: {}", request.getRecordingId());
        
        SpeakerDto.IdentificationResponse response = speakerService.identifySpeaker(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 = SpeakerDto.DetailResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "화자를 찾을 수 없음"
        )
    })
    @GetMapping("/{speakerId}")
    public ResponseEntity<ApiResponse<SpeakerDto.DetailResponse>> getSpeaker(
            @Parameter(description = "화자 ID", required = true)
            @PathVariable String speakerId) {
        log.debug("화자 조회 요청 - speakerId: {}", speakerId);
        
        SpeakerDto.DetailResponse response = speakerService.getSpeaker(speakerId);
        
        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 = SpeakerDto.DetailResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "400", 
            description = "잘못된 요청"
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "화자를 찾을 수 없음"
        )
    })
    @PutMapping("/{speakerId}")
    public ResponseEntity<ApiResponse<SpeakerDto.DetailResponse>> updateSpeaker(
            @Parameter(description = "화자 ID", required = true)
            @PathVariable String speakerId,
            @Valid @RequestBody SpeakerDto.UpdateRequest request) {
        log.info("화자 정보 수정 요청 - speakerId: {}, speakerName: {}", 
                speakerId, request.getSpeakerName());
        
        SpeakerDto.DetailResponse response = speakerService.updateSpeaker(speakerId, 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 = SpeakerDto.ListResponse.class))
        ),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(
            responseCode = "404", 
            description = "녹음을 찾을 수 없음"
        )
    })
    @GetMapping("/recordings/{recordingId}")
    public ResponseEntity<ApiResponse<SpeakerDto.ListResponse>> getRecordingSpeakers(
            @Parameter(description = "녹음 ID", required = true)
            @PathVariable String recordingId) {
        log.debug("녹음별 화자 목록 조회 요청 - recordingId: {}", recordingId);
        
        SpeakerDto.ListResponse response = speakerService.getRecordingSpeakers(recordingId);
        
        return ResponseEntity.ok(ApiResponse.success(response));
    }
}