@startuml
!theme mono

title 실시간 수정 동기화 내부 시퀀스

participant "WebSocket<<E>>" as WebSocket
participant "CollaborationController" as Controller
participant "CollaborationService" as Service
participant "TranscriptService" as TranscriptService
participant "OperationalTransform" as OT
database "Redis Cache<<E>>" as Cache
queue "Event Hub<<E>>" as EventHub

WebSocket -> Controller: onMessage(editOperation)
activate Controller

Controller -> Service: processEdit(meetingId, operation, userId)
activate Service

Service -> Cache: get(meeting:{id}:session)
activate Cache
note right of Cache
활성 세션 정보:
- 참여 사용자 목록
- 현재 문서 버전
- 락 정보
end note
Cache --> Service: sessionData
deactivate Cache

Service -> OT: transform(operation, concurrentOps)
activate OT
note right of OT
Operational Transform:
- 동시 편집 충돌 해결
- 작업 순서 정렬
- 일관성 보장
end note
OT --> Service: transformedOp
deactivate OT

Service -> TranscriptService: applyOperation(meetingId, transformedOp)
activate TranscriptService

TranscriptService -> TranscriptService: updateContent()
note right of TranscriptService
내용 업데이트:
- 버전 증가
- 변경 사항 적용
- 임시 저장
end note

TranscriptService --> Service: updatedVersion
deactivate TranscriptService

Service -> Cache: SET meeting:{id}:version\n(TTL: 1시간)
activate Cache
note right of Cache
  세션 버전 정보 캐싱:
  - TTL: 1시간
  - 버전 정보 업데이트
  - 최신 상태 유지
end note
Cache --> Service: OK
deactivate Cache

Service ->> EventHub: publish(EditOperationEvent)
activate EventHub
note right of EventHub
다른 참여자에게 전파:
- WebSocket 브로드캐스트
- 실시간 동기화
end note
deactivate EventHub

Service --> Controller: SyncResponse
deactivate Service

Controller --> WebSocket: broadcast(editOperation)
deactivate Controller

note over WebSocket
다른 클라이언트에게
실시간 전송
end note

@enduml
