package com.jeesharp.business.modules.setting.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.setting.convert.HelpCategoryConvert;
import com.jeesharp.business.modules.setting.dto.HelpCategorySaveDTO;
import com.jeesharp.business.modules.setting.entity.HelpCategory;
import com.jeesharp.business.modules.setting.service.HelpArticleService;
import com.jeesharp.business.modules.setting.service.HelpCategoryService;
import com.jeesharp.business.modules.setting.vo.HelpCategoryListVO;
import com.jeesharp.business.modules.setting.vo.HelpCategoryQueryVO;
import com.jeesharp.business.modules.setting.vo.HelpCategoryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 帮助分类应用服务
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@Service
@Transactional
@AllArgsConstructor
public class HelpCategoryApplicationService {

    private final HelpCategoryService helpCategoryService;
    private final HelpArticleService helpArticleService;

    /**
     * 获取帮助分类详情
     *
     * @param id ID
     * @return 帮助分类VO
     */
    @Transactional(readOnly = true)
    public HelpCategoryVO getHelpCategory(String id) {
        HelpCategory category = helpCategoryService.get(id);
        HelpCategoryVO vo = HelpCategoryConvert.INSTANCE.toInfoVO(category);
        // 填充文章数量
        fillArticleCount(vo);
        return vo;
    }

    /**
     * 分页查询帮助分类列表
     *
     * @param page  分页对象
     * @param query 查询条件
     * @return 分页结果
     */
    @Transactional(readOnly = true)
    public PageRespVO<HelpCategoryListVO> findPage(Page<HelpCategory> page, HelpCategoryQueryVO query) {
        Page<HelpCategory> result = helpCategoryService.findPage(page, query);
        PageRespVO<HelpCategoryListVO> pageResp = HelpCategoryConvert.INSTANCE.toPageRespVO(result);
        return pageResp;
    }

    /**
     * 根据应用标识查询分类列表
     *
     * @param app 应用标识
     * @return 分类列表
     */
    @Transactional(readOnly = true)
    public List<HelpCategoryVO> listByApp(String app) {
        List<HelpCategory> list = helpCategoryService.listByApp(app);
        List<HelpCategoryVO> voList = HelpCategoryConvert.INSTANCE.toInfoListVO(list);
        // 填充文章数量
        voList.forEach(this::fillArticleCount);
        return voList;
    }

    /**
     * 保存帮助分类
     *
     * @param dto 保存请求
     */
    @Transactional(readOnly = false)
    public void save(HelpCategorySaveDTO dto) {
        HelpCategory category = HelpCategoryConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            helpCategoryService.createCategory(category);
            return;
        }
        helpCategoryService.updateCategory(category);
    }

    /**
     * 删除帮助分类
     *
     * @param ids ID，多个用逗号分隔
     */
    @Transactional(readOnly = false)
    public void delete(String ids) {
        helpCategoryService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 更新状态
     *
     * @param id     ID
     * @param status 状态
     */
    @Transactional(readOnly = false)
    public void updateStatus(String id, Integer status) {
        helpCategoryService.updateStatus(id, status);
    }

    /**
     * 禁用
     *
     * @param id ID
     */
    @Transactional(readOnly = false)
    public void disable(String id) {
        updateStatus(id, 0);
    }

    /**
     * 启用
     *
     * @param id ID
     */
    @Transactional(readOnly = false)
    public void enable(String id) {
        updateStatus(id, 1);
    }

    /**
     * 填充文章数量
     */
    private void fillArticleCount(HelpCategoryVO vo) {
        if (vo != null && StrUtil.isNotBlank(vo.getId())) {
            long count = helpArticleService.listByCategoryId(vo.getId()).stream()
                    .filter(article -> article.getStatus() != null && article.getStatus() == 1)
                    .count();
            vo.setArticleCount((int) count);
        }
    }
}
