package cn.novalon.gym.manage.groupcourse.service.impl;

import cn.novalon.gym.manage.groupcourse.domain.GroupCourseType;
import cn.novalon.gym.manage.groupcourse.repository.IGroupCourseTypeRepository;
import cn.novalon.gym.manage.groupcourse.service.IGroupCourseTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashSet;
import java.util.Set;

@Service
public class GroupCourseTypeService implements IGroupCourseTypeService {

    private static final Logger logger = LoggerFactory.getLogger(GroupCourseTypeService.class);

    private final IGroupCourseTypeRepository groupCourseTypeRepository;

    public GroupCourseTypeService(IGroupCourseTypeRepository groupCourseTypeRepository) {
        this.groupCourseTypeRepository = groupCourseTypeRepository;
    }

    @Override
    public Mono<GroupCourseType> findById(Long id) {
        return groupCourseTypeRepository.findById(id);
    }

    @Override
    public Flux<GroupCourseType> findAll() {
        return groupCourseTypeRepository.findAll(false);
    }

    @Override
    public Flux<GroupCourseType> findAll(boolean includeDeleted) {
        return groupCourseTypeRepository.findAll(includeDeleted);
    }

    @Override
    public Flux<GroupCourseType> findByKeyword(String keyword) {
        return groupCourseTypeRepository.findByKeyword(keyword);
    }

    @Override
    public Flux<GroupCourseType> findByCategory(String category) {
        return groupCourseTypeRepository.findByCategory(category);
    }

    @Override
    public Flux<GroupCourseType> findByCategoryAndKeyword(String category, String keyword) {
        return groupCourseTypeRepository.findByCategoryAndKeyword(category, keyword);
    }

    @Override
    public Mono<GroupCourseType> create(GroupCourseType groupCourseType) {
        return groupCourseTypeRepository.findByTypeName(groupCourseType.getTypeName())
                .flatMap(existing -> Mono.<GroupCourseType>error(new RuntimeException("团课类型名称已存在")))
                .switchIfEmpty(groupCourseTypeRepository.save(groupCourseType))
                .doOnSuccess(type -> logger.info("团课类型创建成功 - id={}, name={}", type.getId(), type.getTypeName()))
                .doOnError(error -> logger.error("团课类型创建失败 - error: {}", error.getMessage()));
    }

    @Override
    public Mono<GroupCourseType> update(Long id, GroupCourseType groupCourseType) {
        return groupCourseTypeRepository.update(groupCourseType)
                .doOnSuccess(type -> logger.info("团课类型更新成功 - id={}", id))
                .doOnError(error -> logger.error("团课类型更新失败 - id={}, error: {}", id, error.getMessage()));
    }

    @Override
    public Mono<Void> delete(Long id) {
        return groupCourseTypeRepository.deleteById(id)
                .doOnSuccess(v -> logger.info("团课类型删除成功 - id={}", id))
                .doOnError(error -> logger.error("团课类型删除失败 - id={}, error: {}", id, error.getMessage()));
    }

    @Override
    public Flux<String> findCategories() {
        return groupCourseTypeRepository.findAll(false)
                .map(GroupCourseType::getCategory)
                .filter(category -> category != null && !category.isEmpty())
                .distinct();
    }
}