package com.unicorn.hgzero.stt.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.unicorn.hgzero.stt.dto.RecordingDto;
import com.unicorn.hgzero.stt.service.RecordingService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDateTime;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * 간단한 녹음 컨트롤러 테스트
 */
@WebMvcTest(RecordingController.class)
@DisplayName("간단한 녹음 컨트롤러 테스트")
class SimpleRecordingControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @MockBean
    private RecordingService recordingService;

    private RecordingDto.PrepareRequest prepareRequest;
    private RecordingDto.PrepareResponse prepareResponse;

    @BeforeEach
    void setUp() {
        prepareRequest = RecordingDto.PrepareRequest.builder()
                .meetingId("MEETING-001")
                .sessionId("SESSION-001")
                .language("ko-KR")
                .build();

        prepareResponse = RecordingDto.PrepareResponse.builder()
                .recordingId("REC-20250123-001")
                .sessionId("SESSION-001")
                .status("READY")
                .streamUrl("wss://api.example.com/stt/v1/ws/stt/SESSION-001")
                .storagePath("recordings/MEETING-001/SESSION-001.wav")
                .estimatedInitTime(1100)
                .build();
    }

    @Test
    @DisplayName("녹음 준비 API 성공")
    void prepareRecording_Success() throws Exception {
        // Given
        when(recordingService.prepareRecording(any(RecordingDto.PrepareRequest.class)))
                .thenReturn(prepareResponse);

        // When & Then
        mockMvc.perform(post("/api/v1/stt/recordings/prepare")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(prepareRequest)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data.recordingId").value("REC-20250123-001"))
                .andExpect(jsonPath("$.data.sessionId").value("SESSION-001"))
                .andExpect(jsonPath("$.data.status").value("READY"));

        verify(recordingService).prepareRecording(any(RecordingDto.PrepareRequest.class));
    }

    @Test
    @DisplayName("녹음 시작 API 성공")
    void startRecording_Success() throws Exception {
        // Given
        String recordingId = "REC-20250123-001";
        RecordingDto.StartRequest startRequest = RecordingDto.StartRequest.builder()
                .startedBy("user001")
                .build();

        RecordingDto.StatusResponse statusResponse = RecordingDto.StatusResponse.builder()
                .recordingId(recordingId)
                .status("RECORDING")
                .startTime(LocalDateTime.now())
                .duration(0)
                .build();

        when(recordingService.startRecording(eq(recordingId), any(RecordingDto.StartRequest.class)))
                .thenReturn(statusResponse);

        // When & Then
        mockMvc.perform(post("/api/v1/stt/recordings/{recordingId}/start", recordingId)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(startRequest)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data.recordingId").value(recordingId))
                .andExpect(jsonPath("$.data.status").value("RECORDING"));

        verify(recordingService).startRecording(eq(recordingId), any(RecordingDto.StartRequest.class));
    }
}