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

import com.unicorn.hgzero.meeting.biz.domain.AgendaSection;
import com.unicorn.hgzero.meeting.infra.gateway.entity.AgendaSectionEntity;
import com.unicorn.hgzero.meeting.infra.gateway.repository.AgendaSectionRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 회의 안건 섹션 서비스
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AgendaSectionService {
    
    private final AgendaSectionRepository agendaSectionRepository;
    
    /**
     * 회의록 ID로 안건 섹션 목록 조회
     * @param minutesId 회의록 ID
     * @return 안건 섹션 도메인 목록
     */
    @Transactional(readOnly = true)
    public List<AgendaSection> getAgendaSectionsByMinutesId(String minutesId) {
        log.info("안건 섹션 목록 조회 - minutesId: {}", minutesId);
        
        List<AgendaSectionEntity> entities = agendaSectionRepository.findByMinutesIdOrderByAgendaNumber(minutesId);
        
        return entities.stream()
                .map(AgendaSectionEntity::toDomain)
                .collect(Collectors.toList());
    }
    
    /**
     * 회의 ID로 안건 섹션 목록 조회
     * @param meetingId 회의 ID
     * @return 안건 섹션 도메인 목록
     */
    @Transactional(readOnly = true)
    public List<AgendaSection> getAgendaSectionsByMeetingId(String meetingId) {
        log.info("안건 섹션 목록 조회 - meetingId: {}", meetingId);
        
        List<AgendaSectionEntity> entities = agendaSectionRepository.findByMeetingIdOrderByAgendaNumber(meetingId);
        
        return entities.stream()
                .map(AgendaSectionEntity::toDomain)
                .collect(Collectors.toList());
    }
    
    /**
     * 안건 섹션 저장
     * @param agendaSection 안건 섹션 도메인
     * @return 저장된 안건 섹션
     */
    @Transactional
    public AgendaSection saveAgendaSection(AgendaSection agendaSection) {
        log.info("안건 섹션 저장 - minutesId: {}, agendaTitle: {}", 
                agendaSection.getMinutesId(), agendaSection.getAgendaTitle());
        
        AgendaSectionEntity entity = AgendaSectionEntity.fromDomain(agendaSection);
        AgendaSectionEntity savedEntity = agendaSectionRepository.save(entity);
        
        return savedEntity.toDomain();
    }
    
    /**
     * 안건 섹션 목록 저장
     * @param agendaSections 안건 섹션 도메인 목록
     * @return 저장된 안건 섹션 목록
     */
    @Transactional
    public List<AgendaSection> saveAgendaSections(List<AgendaSection> agendaSections) {
        log.info("안건 섹션 목록 저장 - 개수: {}", agendaSections.size());
        
        List<AgendaSectionEntity> entities = agendaSections.stream()
                .map(AgendaSectionEntity::fromDomain)
                .collect(Collectors.toList());
        
        List<AgendaSectionEntity> savedEntities = agendaSectionRepository.saveAll(entities);
        
        return savedEntities.stream()
                .map(AgendaSectionEntity::toDomain)
                .collect(Collectors.toList());
    }
}