package com.bizzan.bitrade.controller.cms;

import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.SysHelpCategory;
import com.bizzan.bitrade.service.SysHelpCategoryService;
import com.bizzan.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帮助分类管理
 */
@RestController
@RequestMapping("/cms/help-category")
@Slf4j
public class SysHelpCategoryController extends BaseAdminController {

    @Autowired
    private SysHelpCategoryService sysHelpCategoryService;

    /**
     * 全量列表（下拉选择用），可按 scene 过滤
     */
    @GetMapping("/list")
    public MessageResult list(@RequestParam(required = false) String scene) {
        List<SysHelpCategory> list = (scene != null && !scene.isEmpty())
                ? sysHelpCategoryService.findEnabledByScene(scene)
                : sysHelpCategoryService.findEnabled();
        return success(list);
    }

    /**
     * 分页查询
     */
    @PostMapping("/page-query")
    public MessageResult pageQuery(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize
    ) {
        Pageable pageable = new PageRequest(pageNo - 1, pageSize,
                new Sort(Sort.Direction.DESC, "sort"));
        Page<SysHelpCategory> page = sysHelpCategoryService.findAll(pageable);
        return success(page);
    }

    /**
     * 新增分类
     */
    @PostMapping("/create")
    public MessageResult create(
            @RequestParam String code,
            @RequestParam String nameZh,
            @RequestParam String nameEn,
            @RequestParam(defaultValue = "HELP") String scene,
            @RequestParam(defaultValue = "0") Integer sort,
            @RequestParam(defaultValue = "1") Integer status,
            @RequestParam(required = false) String remark
    ) {
        if (sysHelpCategoryService.findByCode(code) != null) {
            return error("分类编码已存在: " + code);
        }
        SysHelpCategory category = new SysHelpCategory();
        category.setCode(code.toUpperCase().trim());
        category.setNameZh(nameZh);
        category.setNameEn(nameEn);
        category.setScene(scene);
        category.setSort(sort);
        category.setStatus(status);
        category.setRemark(remark);
        return success(sysHelpCategoryService.save(category));
    }

    /**
     * 修改分类
     */
    @PostMapping("/update")
    public MessageResult update(
            @RequestParam Long id,
            @RequestParam String nameZh,
            @RequestParam String nameEn,
            @RequestParam String scene,
            @RequestParam Integer sort,
            @RequestParam Integer status,
            @RequestParam(required = false) String remark
    ) {
        SysHelpCategory category = sysHelpCategoryService.findOne(id);
        Assert.notNull(category, "记录不存在");
        category.setNameZh(nameZh);
        category.setNameEn(nameEn);
        category.setScene(scene);
        category.setSort(sort);
        category.setStatus(status);
        category.setRemark(remark);
        return success(sysHelpCategoryService.save(category));
    }

    /**
     * 批量删除
     */
    @PostMapping("/deletes")
    public MessageResult deletes(@RequestParam String ids) {
        String[] idArr = ids.split(",");
        Long[] longIds = new Long[idArr.length];
        for (int i = 0; i < idArr.length; i++) {
            longIds[i] = Long.parseLong(idArr[i].trim());
        }
        sysHelpCategoryService.deleteBatch(longIds);
        return success();
    }
}
