package com.warmer.web.controller;

import com.warmer.base.util.R;
import com.warmer.base.util.GraphPageRecord;
import com.warmer.web.service.KGGraphService;
import com.warmer.web.request.GraphQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/kg/domain")
public class GraphDomainController extends BaseController {

    @Autowired
    private KGGraphService kgGraphService;

    /**
     * 领域分页（支持按关键字过滤）
     * 请求体支持 GraphQuery.keywords，pageIndex，pageSize
     */
    @PostMapping("/page")
    @ResponseBody
    public R<GraphPageRecord<HashMap<String, Object>>> page(@RequestBody GraphQuery query) {
        GraphPageRecord<HashMap<String, Object>> data = kgGraphService.getPageDomain(query);
        return R.success(data, "操作成功");
    }

    /**
     * 删除指定领域（标签）
     */
    @DeleteMapping("/{domain}")
    @ResponseBody
    public R<?> delete(@PathVariable("domain") String domain) {
        if (StringUtils.isBlank(domain)) {
            return R.error("领域名称不能为空");
        }
        kgGraphService.deleteKGDomain(domain);
        return R.success(null, "删除成功");
    }

    /**
     * 重命名领域（标签）
     * 请求体：{ "oldName": "...", "newName": "..." }
     */
    @PutMapping("/rename")
    @ResponseBody
    public R<?> rename(@RequestBody HashMap<String, String> body) {
        String oldName = body.get("oldName");
        String newName = body.get("newName");
        if (StringUtils.isBlank(oldName) || StringUtils.isBlank(newName)) {
            return R.error("oldName/newName 不能为空");
        }
        if (StringUtils.equals(oldName, newName)) {
            return R.error("新名称不能与旧名称相同");
        }
        kgGraphService.renameKGDomain(oldName, newName);
        return R.success(null, "重命名成功");
    }
}