package com.unicorn.hgzero.meeting.infra.gateway;

import com.unicorn.hgzero.meeting.biz.domain.MinutesSection;
import com.unicorn.hgzero.meeting.biz.usecase.out.MinutesSectionReader;
import com.unicorn.hgzero.meeting.biz.usecase.out.MinutesSectionWriter;
import com.unicorn.hgzero.meeting.infra.gateway.entity.MinutesSectionEntity;
import com.unicorn.hgzero.meeting.infra.gateway.repository.MinutesSectionJpaRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 회의록 섹션 Gateway 구현체
 * MinutesSectionReader, MinutesSectionWriter 인터페이스 구현
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MinutesSectionGateway implements MinutesSectionReader, MinutesSectionWriter {

    private final MinutesSectionJpaRepository sectionJpaRepository;

    @Override
    public Optional<MinutesSection> findById(String sectionId) {
        return sectionJpaRepository.findById(sectionId)
                .map(MinutesSectionEntity::toDomain);
    }

    @Override
    public List<MinutesSection> findByMinutesIdOrderByOrder(String minutesId) {
        return sectionJpaRepository.findByMinutesIdOrderByOrderAsc(minutesId).stream()
                .map(MinutesSectionEntity::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<MinutesSection> findByMinutesIdAndType(String minutesId, String type) {
        return sectionJpaRepository.findByMinutesIdAndType(minutesId, type).stream()
                .map(MinutesSectionEntity::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<MinutesSection> findByMinutesIdAndVerified(String minutesId, Boolean verified) {
        return sectionJpaRepository.findByMinutesIdAndVerified(minutesId, verified).stream()
                .map(MinutesSectionEntity::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<MinutesSection> findByMinutesIdAndLocked(String minutesId, Boolean locked) {
        return sectionJpaRepository.findByMinutesIdAndLocked(minutesId, locked).stream()
                .map(MinutesSectionEntity::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<MinutesSection> findByLockedBy(String lockedBy) {
        return sectionJpaRepository.findByLockedBy(lockedBy).stream()
                .map(MinutesSectionEntity::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public MinutesSection save(MinutesSection section) {
        // 기존 엔티티 조회 (update) 또는 새로 생성 (insert)
        MinutesSectionEntity entity = sectionJpaRepository.findById(section.getSectionId())
                .orElse(null);

        if (entity != null) {
            // 기존 엔티티 업데이트 (minutes 연관관계 유지)
            if (section.getLocked() != null && section.getLocked()) {
                entity.lock(section.getLockedBy());
            } else if (section.getLocked() != null && !section.getLocked()) {
                entity.unlock();
            }
            if (section.getVerified() != null && section.getVerified()) {
                entity.verify();
            }
        } else {
            // 새 엔티티 생성
            entity = MinutesSectionEntity.fromDomain(section);
        }

        MinutesSectionEntity savedEntity = sectionJpaRepository.save(entity);
        return savedEntity.toDomain();
    }

    @Override
    public void delete(String sectionId) {
        sectionJpaRepository.deleteById(sectionId);
    }
}
