package com.jeesharp.business.setting.controller;

import com.jeesharp.business.modules.setting.application.HelpArticleApplicationService;
import com.jeesharp.business.modules.setting.application.HelpCategoryApplicationService;
import com.jeesharp.business.modules.setting.vo.HelpArticleListVO;
import com.jeesharp.business.modules.setting.vo.HelpArticleVO;
import com.jeesharp.business.modules.setting.vo.HelpCategoryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动端帮助中心接口。
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/setting/helpCenter")
@Tag(name = "移动端-帮助中心")
@AllArgsConstructor
public class HelpCenterMobileController {

    private final HelpCategoryApplicationService helpCategoryApplicationService;
    private final HelpArticleApplicationService helpArticleApplicationService;

    @Operation(summary = "分类列表")
    @GetMapping("/categories")
    public AjaxResult<List<HelpCategoryVO>> categories(
        @RequestParam(value = "app", defaultValue = "WECHAT_MINI") String app
    ) {
        return AjaxResult.okData(helpCategoryApplicationService.listByApp(app));
    }

    @Operation(summary = "文章列表")
    @GetMapping("/articles")
    public AjaxResult<List<HelpArticleVO>> articles(
        @RequestParam(value = "app", defaultValue = "WECHAT_MINI") String app,
        @RequestParam(value = "categoryId", required = false) String categoryId
    ) {
        if (categoryId != null && !categoryId.isBlank()) {
            return AjaxResult.okData(helpArticleApplicationService.listByCategoryId(categoryId));
        }
        return AjaxResult.okData(helpArticleApplicationService.listByApp(app));
    }

    @Operation(summary = "文章详情")
    @GetMapping("/article/detail")
    public AjaxResult<HelpArticleVO> articleDetail(@RequestParam("id") String id) {
        helpArticleApplicationService.incrementViewCount(id);
        return AjaxResult.okData(helpArticleApplicationService.getHelpArticle(id));
    }

    @Operation(summary = "常见问题")
    @GetMapping("/faq")
    public AjaxResult<List<HelpArticleListVO>> faq(
        @RequestParam(value = "app", defaultValue = "WECHAT_MINI") String app
    ) {
        return AjaxResult.okData(helpArticleApplicationService.listFaqByApp(app));
    }

    @Operation(summary = "点赞文章")
    @PostMapping("/article/like")
    public AjaxResult<Void> like(@RequestParam("id") String id) {
        helpArticleApplicationService.incrementLikeCount(id);
        return AjaxResult.ok("点赞成功");
    }

    @Operation(summary = "帮助中心首页")
    @GetMapping("/index")
    public AjaxResult<Map<String, Object>> index(
        @RequestParam(value = "app", defaultValue = "WECHAT_MINI") String app
    ) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("categories", helpCategoryApplicationService.listByApp(app));
        data.put("faq", helpArticleApplicationService.listFaqByApp(app));
        return AjaxResult.okData(data);
    }
}
