package com.ghb.base.business.controller;

import com.ghb.base.common.api.vo.Result;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 业务模块测试接口，用于验证 ghb-module-business 已被单体和微服务启动包加载。
 */
@RestController
@RequestMapping("/business/test")
public class BusinessTestController {

    /**
     * 查询业务模块测试状态。
     *
     * @return 业务模块加载状态
     */
    @GetMapping
    public Result<Map<String, Object>> getStatus() {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("module", "ghb-module-business");
        data.put("method", "GET");
        data.put("status", "loaded");
        return Result.ok(data);
    }

    /**
     * 创建业务模块测试数据。
     *
     * @param requestBody 请求内容
     * @return 回显后的测试数据
     */
    @PostMapping
    public Result<Map<String, Object>> create(@RequestBody(required = false) Map<String, Object> requestBody) {
        return Result.ok(buildEcho("POST", null, requestBody));
    }

    /**
     * 更新业务模块测试数据。
     *
     * @param id 测试资源 ID
     * @param requestBody 请求内容
     * @return 回显后的测试数据
     */
    @PutMapping("/{id}")
    public Result<Map<String, Object>> update(@PathVariable String id,
                                              @RequestBody(required = false) Map<String, Object> requestBody) {
        return Result.ok(buildEcho("PUT", id, requestBody));
    }

    /**
     * 删除业务模块测试数据。
     *
     * @param id 测试资源 ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Map<String, Object>> delete(@PathVariable String id) {
        return Result.ok(buildEcho("DELETE", id, null));
    }

    private Map<String, Object> buildEcho(String method, String id, Map<String, Object> requestBody) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("module", "ghb-module-business");
        data.put("method", method);
        if (id != null) {
            data.put("id", id);
        }
        if (requestBody != null) {
            data.put("body", requestBody);
        }
        return data;
    }
}
