package com.unicorn.hgzero.meeting.biz.service;

import com.unicorn.hgzero.meeting.biz.domain.Meeting;
import com.unicorn.hgzero.meeting.biz.usecase.in.meeting.ApplyTemplateUseCase;
import com.unicorn.hgzero.meeting.biz.usecase.out.MeetingReader;
import com.unicorn.hgzero.meeting.biz.usecase.out.MeetingWriter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 회의록 템플릿 적용 서비스
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class ApplyTemplateService implements ApplyTemplateUseCase {

    private final MeetingReader meetingReader;
    private final MeetingWriter meetingWriter;

    @Override
    public Meeting applyTemplate(ApplyTemplateCommand command) {
        log.debug("템플릿 적용 시작 - meetingId: {}, templateId: {}", 
                command.meetingId(), command.templateId());

        // 회의 조회
        Meeting meeting = meetingReader.findById(command.meetingId())
                .orElseThrow(() -> new IllegalArgumentException("회의를 찾을 수 없습니다: " + command.meetingId()));

        // 템플릿 적용 (회의 도메인에서 처리)
        meeting.applyTemplate(command.templateId());

        // 회의 정보 업데이트
        Meeting updatedMeeting = meetingWriter.save(meeting);

        log.debug("템플릿 적용 완료 - meetingId: {}, templateId: {}", 
                command.meetingId(), command.templateId());

        return updatedMeeting;
    }
}