package com.unicorn.hgzero.ai.infra.controller;

import com.unicorn.hgzero.ai.biz.domain.RelatedMinutes;
import com.unicorn.hgzero.ai.biz.usecase.RelatedTranscriptSearchUseCase;
import com.unicorn.hgzero.ai.infra.dto.response.RelatedTranscriptsResponse;
import com.unicorn.hgzero.ai.infra.dto.common.RelatedTranscriptDto;
import com.unicorn.hgzero.common.dto.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 관련 회의록 조회 Controller
 * GET /api/transcripts/{meetingId}/related
 */
@RestController
@RequestMapping("/api/ai/transcripts")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "Relation", description = "관련 회의록 조회 API")
public class RelationController {

    private final RelatedTranscriptSearchUseCase relatedTranscriptSearchUseCase;

    @GetMapping("/{meetingId}/related")
    @Operation(summary = "관련 회의록 조회", description = "벡터 유사도 검색을 통해 관련된 회의록을 찾아 반환합니다")
    public ResponseEntity<ApiResponse<RelatedTranscriptsResponse>> findRelatedTranscripts(
            @PathVariable String meetingId,
            @RequestParam String transcriptId,
            @RequestParam(defaultValue = "5") int limit) {

        log.info("관련 회의록 조회 요청 - meetingId: {}, transcriptId: {}, limit: {}", meetingId, transcriptId, limit);

        List<RelatedMinutes> relatedMinutes = relatedTranscriptSearchUseCase.findRelatedTranscripts(
            meetingId,
            transcriptId,
            limit
        );

        RelatedTranscriptsResponse response = RelatedTranscriptsResponse.builder()
                .relatedTranscripts(relatedMinutes.stream()
                        .map(r -> RelatedTranscriptDto.builder()
                                .transcriptId(r.getTranscriptId())
                                .title(r.getTitle())
                                .date(r.getDate())
                                .participants(r.getParticipants())
                                .relevanceScore(r.getRelevanceScore())
                                .commonKeywords(r.getCommonKeywords())
                                .link(r.getLink())
                                .build())
                        .collect(Collectors.toList()))
                .totalCount(relatedMinutes.size())
                .build();

        return ResponseEntity.ok(ApiResponse.success(response));
    }
}
