@startuml
!theme mono

title 템플릿 선택 내부 시퀀스

participant "API Gateway<<E>>" as Gateway
participant "MeetingController" as Controller
participant "MeetingService" as Service
participant "Meeting" as Domain
participant "TemplateService" as TemplateService
participant "MeetingRepository" as Repository
database "PostgreSQL<<E>>" as DB
database "Redis Cache<<E>>" as Cache

Gateway -> Controller: PUT /api/meetings/{id}/template
activate Controller

Controller -> Service: applyTemplate(meetingId, templateId)
activate Service

Service -> Cache: get(meeting:{id})
activate Cache
Cache --> Service: null (cache miss)
deactivate Cache

Service -> Repository: findById(meetingId)
activate Repository
Repository -> DB: 회의 정보 조회\n(회의ID 기준)
activate DB
DB --> Repository: meeting_row
deactivate DB
Repository --> Service: Meeting entity
deactivate Repository

Service -> TemplateService: getTemplate(templateId)
activate TemplateService
note right of TemplateService
템플릿 서비스에서
템플릿 구조 조회
end note
TemplateService --> Service: Template
deactivate TemplateService

Service -> Domain: applyTemplate(template)
activate Domain

Domain -> Domain: validateTemplate()
note right of Domain
도메인 규칙:
- 템플릿 호환성 검증
- 기존 내용과 병합 규칙
end note

Domain --> Service: updated Meeting
deactivate Domain

Service -> Repository: save(meeting)
activate Repository
Repository -> DB: 회의 템플릿 업데이트\n(템플릿ID)
activate DB
DB --> Repository: affected_rows
deactivate DB
Repository --> Service: savedMeeting
deactivate Repository

Service -> Cache: SET meeting:{id}\n(TTL: 10분)
activate Cache
note right of Cache
  회의 정보 캐싱:
  - TTL: 10분
  - 자동 만료
end note
Cache --> Service: OK
deactivate Cache

Service --> Controller: MeetingResponse
deactivate Service

Controller --> Gateway: 200 OK
deactivate Controller

@enduml
