package com.microservices.system.api;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.constant.ServiceNameConstants;
import com.microservices.common.core.constant.TokenConstants;
import com.microservices.system.api.factory.RemoteGatewayFallbackFactory;
import com.microservices.system.api.model.GraphqlQueryVo;
import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 网关
 *
 * @author microservices
 */
@Component
@FeignClient(contextId = "remoteGatewayService", value = ServiceNameConstants.GATEWAY_SERVICE, fallbackFactory = RemoteGatewayFallbackFactory.class)
public interface RemoteGatewayService {
    /**
     * 登录Sentinel
     *
     * @return 响应
     */
    @PostMapping("/sentinel/auth/login")
    Response loginSentinel(@RequestParam("username") String username, @RequestParam("password") String password);

    /**
     * 登录Rancher
     *
     * @return 响应
     */
    @PostMapping("/rancher/v3-public/localProviders/local?action=login")
    Response loginRancher(@RequestBody JSONObject loginBody);

    /**
     * 登录Nacos
     *
     * @return 响应
     */
    @PostMapping(value = "/nacos/v1/auth/users/login", consumes = {"application/x-www-form-urlencoded"})
    Response loginNacos(Map<String, ?> formParams);

    /**
     * 登录Portainer
     *
     * @return 响应
     */
    @PostMapping("/portainer/api/auth")
    Response loginPortainer(@RequestBody JSONObject loginBody);

    @GetMapping("/prometheus/api/v1/query")
    JSONObject getPrometheusQueryRes(@RequestParam("query") String query,
                                     @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/prometheus/api/v1/query_range")
    JSONObject getPrometheusQueryRangeRes(@RequestParam("query") String query,
                                          @RequestParam("step") Long step,
                                          @RequestParam("start") Long start,
                                          @RequestParam("end") Long end,
                                          @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @PostMapping("/skywalking/graphql")
    JSONObject executeSkywalkingGql(@RequestBody GraphqlQueryVo gqlBody);

    @GetMapping("/sentinel/app/briefinfos.json")
    JSONObject getSentinelAppList(@RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/nacos/v1/ns/catalog/services?pageNo=1&pageSize=999")
    JSONObject getNacosServiceList(@RequestParam("serviceNameParam") String serviceNameParam, @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/nacos/v1/cs/configs?types=yaml&search=blur&group=&pageNo=1&pageSize=10")
    JSONObject getGatewayNacosConfigList(@RequestParam("dataId") String dataId, @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/nacos/v1/cs/listener")
    JSONObject getNacosConfigList(@RequestParam("ip") String ip, @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/nacos/v1/cs/configs")
    String getNacosConfigDetail(@RequestParam("dataId") String dataId, @RequestParam("group") String group, @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @PostMapping(value = "/nacos/v1/cs/configs", consumes = {"application/x-www-form-urlencoded"})
    Response updateNacosConfig(Map<String, ?> formParams, @RequestHeader(TokenConstants.AUTHENTICATION) String userKey);

    @PostMapping("/es/{esIndex}/_search")
    JSONObject executeElasticsearchQuery(@PathVariable("esIndex") String esIndex, @RequestBody JSONObject esBody);

    @PostMapping("/es/{esIndex}/_doc/{docId}/_update")
    JSONObject executeElasticsearchUpdate(@PathVariable("esIndex") String esIndex,@PathVariable("docId") String docId,  @RequestBody JSONObject esBody);

    @PostMapping("/gns/notification/gitlink")
    JSONObject sendNotification(@RequestBody JSONObject body);
}
