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

import com.unicorn.hgzero.ai.biz.domain.Suggestion;
import com.unicorn.hgzero.ai.biz.usecase.SuggestionUseCase;
import com.unicorn.hgzero.ai.infra.dto.request.DiscussionSuggestionRequest;
import com.unicorn.hgzero.ai.infra.dto.request.DecisionSuggestionRequest;
import com.unicorn.hgzero.ai.infra.dto.response.DiscussionSuggestionResponse;
import com.unicorn.hgzero.ai.infra.dto.response.DecisionSuggestionResponse;
import com.unicorn.hgzero.ai.infra.dto.common.DiscussionSuggestionDto;
import com.unicorn.hgzero.ai.infra.dto.common.DecisionSuggestionDto;
import com.unicorn.hgzero.common.dto.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
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.*;

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

/**
 * 논의사항/결정사항 제안 Controller
 * POST /api/suggestions/discussion
 * POST /api/suggestions/decision
 */
@RestController
@RequestMapping("/api/ai/suggestions")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "Suggestion", description = "논의사항/결정사항 제안 API")
public class SuggestionController {

    private final SuggestionUseCase suggestionUseCase;

    @PostMapping("/discussion")
    @Operation(summary = "논의사항 제안", description = "현재 회의 진행 상황을 분석하여 추가로 논의하면 좋을 주제를 제안합니다")
    public ResponseEntity<ApiResponse<DiscussionSuggestionResponse>> suggestDiscussion(
            @Valid @RequestBody DiscussionSuggestionRequest request) {

        log.info("논의사항 제안 요청 - meetingId: {}", request.getMeetingId());

        List<Suggestion> suggestions = suggestionUseCase.suggestDiscussions(
            request.getMeetingId(),
            request.getTranscriptText()
        );

        DiscussionSuggestionResponse response = DiscussionSuggestionResponse.builder()
                .suggestions(suggestions.stream()
                        .map(s -> DiscussionSuggestionDto.builder()
                                .id(s.getId())
                                .topic(s.getContent())
                                .reason(s.getReason())
                                .priority(s.getPriority())
                                .relatedAgenda(s.getRelatedAgenda())
                                .estimatedTime(s.getEstimatedTime())
                                .build())
                        .collect(Collectors.toList()))
                .totalCount(suggestions.size())
                .timestamp(LocalDateTime.now())
                .build();

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

    @PostMapping("/decision")
    @Operation(summary = "결정사항 제안", description = "회의록 텍스트에서 결정사항 패턴을 감지하여 제안합니다")
    public ResponseEntity<ApiResponse<DecisionSuggestionResponse>> suggestDecision(
            @Valid @RequestBody DecisionSuggestionRequest request) {

        log.info("결정사항 제안 요청 - meetingId: {}", request.getMeetingId());

        List<Suggestion> suggestions = suggestionUseCase.suggestDecisions(
            request.getMeetingId(),
            request.getTranscriptText()
        );

        DecisionSuggestionResponse response = DecisionSuggestionResponse.builder()
                .suggestions(suggestions.stream()
                        .map(s -> DecisionSuggestionDto.builder()
                                .id(s.getId())
                                .content(s.getContent())
                                .category(s.getCategory())
                                .decisionMaker("") // TODO: Extract from suggestion
                                .participants(s.getParticipants())
                                .confidence(s.getConfidence())
                                .extractedFrom(s.getExtractedFrom())
                                .context(s.getContext())
                                .build())
                        .collect(Collectors.toList()))
                .totalCount(suggestions.size())
                .timestamp(LocalDateTime.now())
                .build();

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