@startuml
!theme mono

title STT Service - 음성-텍스트 변환 (실시간 전용)

participant "Frontend<<E>>" as Frontend
participant "API Gateway<<E>>" as Gateway
participant "TranscriptController" as Controller
participant "TranscriptService" as Service
participant "TranscriptionEngine" as Engine
participant "RecordingRepository" as RecordingRepo
participant "TranscriptRepository" as TranscriptRepo
participant "AzureSpeechClient" as AzureClient
database "STT DB" as DB
database "Azure Blob Storage<<E>>" as BlobStorage
queue "Azure Event Hubs<<E>>" as EventHub

== 음성 데이터 스트리밍 수신 ==

Frontend -> Gateway: POST /api/transcripts/stream\n(audioData, recordingId, timestamp)
activate Gateway

Gateway -> Controller: 음성 스트림 요청
activate Controller

Controller -> Service: processAudioStream(audioData, recordingId)
activate Service

Service -> Engine: streamingTranscribe(audioData)
activate Engine

    Engine -> AzureClient: recognizeAsync(audioData)
    activate AzureClient

    AzureClient -> AzureClient: 실시간 음성 인식 수행
    note right
      Azure Speech 설정:
      - Mode: Continuous
      - 언어: ko-KR
      - 타임스탬프 자동 기록
      - 신뢰도 점수 계산
      - Profanity filter
    end note

    AzureClient -> BlobStorage: 음성 파일 저장\n(chunk 단위 저장)
    activate BlobStorage
    BlobStorage --> AzureClient: 저장 완료
    deactivate BlobStorage

    AzureClient --> Engine: RecognitionResult\n(text, confidence, timestamp, duration)
    deactivate AzureClient

    == 정확도 검증 및 처리 ==

    Engine -> Engine: validateConfidence(result)
    note right
      신뢰도 검증:
      - Threshold: 0.7 (70%)
      - confidence >= 80%: 정상 처리
      - 60% <= confidence < 80%: 검토 권장
      - confidence < 60%: 경고 플래그 설정
    end note

    Engine --> Service: transcription segment
    deactivate Engine

    == 변환 결과 저장 ==

    Service -> TranscriptRepo: createTranscript(recordingId, segment)
    activate TranscriptRepo

    TranscriptRepo -> DB: 변환 결과 저장\n(텍스트ID, 녹음ID, 텍스트, 신뢰도, 타임스탬프, 경고플래그)
    activate DB
    DB --> TranscriptRepo: transcriptId 반환
    deactivate DB

    TranscriptRepo --> Service: TranscriptEntity 반환
    deactivate TranscriptRepo

    == 이벤트 발행 ==

    Service -> EventHub: TranscriptSegmentReady 이벤트 발행
    activate EventHub
    note right of EventHub
      이벤트 데이터:
      - transcriptId
      - recordingId
      - meetingId
      - text
      - timestamp
      - confidence
    end note
    EventHub --> Service: 발행 완료
    deactivate EventHub

    Service --> Controller: TranscriptResponse\n(transcriptId, text, confidence, warningFlag)
    deactivate Service

    Controller --> Gateway: 200 OK\n(transcriptId, text, timestamp, confidence)
    deactivate Controller

    Gateway --> Frontend: 실시간 자막 응답
    deactivate Gateway

note over Frontend, EventHub
**처리 시간:**
- Azure STT 처리: 1-3초
- DB 저장: ~100ms
- Event 발행: ~50ms
- 총 처리 시간: 1-3초

**정확도 경고 기준:**
- < 60%: 수동 수정 권장 (경고 플래그)
- 60-80%: 검토 권장
- >= 80%: 정상
end note

@enduml
