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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.setting.convert.HelpArticleConvert;
import com.jeesharp.business.modules.setting.dto.HelpArticleSaveDTO;
import com.jeesharp.business.modules.setting.entity.HelpArticle;
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.HelpArticleListVO;
import com.jeesharp.business.modules.setting.vo.HelpArticleQueryVO;
import com.jeesharp.business.modules.setting.vo.HelpArticleVO;
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 HelpArticleApplicationService {

    private final HelpArticleService helpArticleService;
    private final HelpCategoryService helpCategoryService;

    /**
     * 获取帮助文章详情
     *
     * @param id ID
     * @return 帮助文章VO
     */
    @Transactional(readOnly = true)
    public HelpArticleVO getHelpArticle(String id) {
        HelpArticle article = helpArticleService.get(id);
        HelpArticleVO vo = HelpArticleConvert.INSTANCE.toInfoVO(article);
        // 填充分类名称
        fillCategoryName(vo, article.getCategoryId());
        return vo;
    }

    /**
     * 分页查询帮助文章列表
     *
     * @param page  分页对象
     * @param query 查询条件
     * @return 分页结果
     */
    @Transactional(readOnly = true)
    public PageRespVO<HelpArticleListVO> findPage(Page<HelpArticle> page, HelpArticleQueryVO query) {
        Page<HelpArticle> result = helpArticleService.findPage(page, query);
        PageRespVO<HelpArticleListVO> pageResp = HelpArticleConvert.INSTANCE.toPageRespVO(result);
        return pageResp;
    }

    /**
     * 根据应用标识查询文章列表
     *
     * @param app 应用标识
     * @return 文章列表
     */
    @Transactional(readOnly = true)
    public List<HelpArticleVO> listByApp(String app) {
        List<HelpArticle> list = helpArticleService.listByApp(app);
        List<HelpArticleVO> voList = HelpArticleConvert.INSTANCE.toInfoListVO(list);
        // 填充分类名称
        voList.forEach(vo -> fillCategoryName(vo, vo.getCategoryId()));
        return voList;
    }

    /**
     * 根据分类ID查询文章列表
     *
     * @param categoryId 分类ID
     * @return 文章列表
     */
    @Transactional(readOnly = true)
    public List<HelpArticleVO> listByCategoryId(String categoryId) {
        List<HelpArticle> list = helpArticleService.listByCategoryId(categoryId);
        List<HelpArticleVO> voList = HelpArticleConvert.INSTANCE.toInfoListVO(list);
        // 填充分类名称
        voList.forEach(vo -> fillCategoryName(vo, vo.getCategoryId()));
        return voList;
    }

    /**
     * 根据应用标识查询常见问题列表
     *
     * @param app 应用标识
     * @return 常见问题列表
     */
    @Transactional(readOnly = true)
    public List<HelpArticleListVO> listFaqByApp(String app) {
        List<HelpArticle> list = helpArticleService.listFaqByApp(app);
        List<HelpArticleListVO> voList = HelpArticleConvert.INSTANCE.toListVO(list);
        // 填充分类名称
        voList.forEach(vo -> fillCategoryName(vo, vo.getCategoryId()));
        return voList;
    }

    /**
     * 保存帮助文章
     *
     * @param dto 保存请求
     */
    @Transactional(readOnly = false)
    public void save(HelpArticleSaveDTO dto) {
        HelpArticle article = HelpArticleConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            helpArticleService.createArticle(article);
            return;
        }
        helpArticleService.updateArticle(article);
    }

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

    /**
     * 更新状态
     *
     * @param id     ID
     * @param status 状态
     */
    @Transactional(readOnly = false)
    public void updateStatus(String id, Integer status) {
        helpArticleService.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);
    }

    /**
     * 增加浏览次数
     *
     * @param id 文章ID
     */
    @Transactional(readOnly = false)
    public void incrementViewCount(String id) {
        helpArticleService.incrementViewCount(id);
    }

    /**
     * 增加点赞次数
     *
     * @param id 文章ID
     */
    @Transactional(readOnly = false)
    public void incrementLikeCount(String id) {
        helpArticleService.incrementLikeCount(id);
    }

    /**
     * 填充分类名称
     */
    private void fillCategoryName(HelpArticleVO vo, String categoryId) {
        if (vo != null && StrUtil.isNotBlank(categoryId)) {
            HelpCategory category = helpCategoryService.getById(categoryId);
            if (category != null) {
                vo.setCategoryName(category.getCategoryName());
            }
        }
    }

    private void fillCategoryName(HelpArticleListVO vo, String categoryId) {
        if (vo != null && StrUtil.isNotBlank(categoryId)) {
            HelpCategory category = helpCategoryService.getById(categoryId);
            if (category != null) {
                vo.setCategoryName(category.getCategoryName());
            }
        }
    }
}
