package com.reading.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reading.platform.common.enums.ErrorCode;
import com.reading.platform.common.exception.BusinessException;
import com.reading.platform.entity.Theme;
import com.reading.platform.mapper.ThemeMapper;
import com.reading.platform.service.ThemeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 主题服务实现类
 */
@Service
@RequiredArgsConstructor
public class ThemeServiceImpl implements ThemeService {

    private final ThemeMapper themeMapper;

    @Override
    public List<Theme> getAllThemes(Boolean enabledOnly) {
        LambdaQueryWrapper<Theme> wrapper = new LambdaQueryWrapper<>();
        if (Boolean.TRUE.equals(enabledOnly)) {
            wrapper.eq(Theme::getIsEnabled, 1);
        }
        wrapper.orderByAsc(Theme::getSortOrder);
        return themeMapper.selectList(wrapper);
    }

    @Override
    public Theme getThemeById(String id) {
        Theme theme = themeMapper.selectById(id);
        if (theme == null) {
            throw new BusinessException(ErrorCode.DATA_NOT_FOUND, "Theme not found");
        }
        return theme;
    }

    @Override
    public Theme createTheme(Theme theme) {
        themeMapper.insert(theme);
        return theme;
    }

    @Override
    public Theme updateTheme(String id, Theme theme) {
        getThemeById(id);
        theme.setId(id);
        themeMapper.updateById(theme);
        return themeMapper.selectById(id);
    }

    @Override
    public void deleteTheme(String id) {
        themeMapper.deleteById(id);
    }
}
