package com.microservices.system.api;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.constant.Constants;
import com.microservices.common.core.constant.SecurityConstants;
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.factory.RemoteRancherFallbackFactory;
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 = "RemoteRancherService", value = ServiceNameConstants.GATEWAY_SERVICE, fallbackFactory = RemoteRancherFallbackFactory.class)
public interface RemoteRancherService {
    @GetMapping("/rancher/v3/clusters/{cluster_id}/namespaces")
    Response getK8sNamespaceList(@PathVariable("cluster_id") String cluster_id,
                                 @RequestHeader(TokenConstants.AUTHENTICATION) String token);
    @GetMapping("/rancher/k8s/clusters/{cluster_id}/api/v1/namespaces")
    Response getK8sNamespaceListNative(@PathVariable("cluster_id") String cluster_id,
                                 @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.daemonsets?exclude=metadata.managedFields")
    Response getK8sDaemonsetsList(@PathVariable("cluster_id") String cluster_id,
                                  @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.deployments?exclude=metadata.managedFields")
    Response getK8sDeployList(@PathVariable("cluster_id") String cluster_id,
                              @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/apps.statefulsets?exclude=metadata.managedFields")
    Response getK8sStatefulList(@PathVariable("cluster_id") String cluster_id,
                                @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/nodes?exclude=metadata.managedFields")
    Response getK8sClusterNodeList(@PathVariable("cluster_id") String cluster_id,
                                   @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/{fullKind}/{namespace}/{applicationName}")
    Response getK8sApplicationDetail(@PathVariable("cluster_id") String cluster_id,
                                     @PathVariable("fullKind") String fullKind,
                                     @PathVariable("namespace") String namespace,
                                     @PathVariable("applicationName") String applicationName,
                                     @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/pods/{namespace}")
    Response getK8sPodListByNamespace(@PathVariable("cluster_id") String cluster_id,
                                      @PathVariable("namespace") String namespace,
                                      @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/pods")
    Response getK8sPodList(@PathVariable("cluster_id") String cluster_id,
                           @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/services/{nginxApplicationId}")
    Response getK8sServiceByApplicationId(@PathVariable("cluster_id") String cluster_id,
                                          @PathVariable("nginxApplicationId") String nginxApplicationId,
                                          @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @DeleteMapping("/rancher/v1/pods/{namespace}/{podName}")
    Response deletePod(@PathVariable("namespace") String namespace,
                       @PathVariable("podName") String podName,
                       @RequestBody JSONObject deletePodBody,
                       @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @PutMapping("/rancher/v1/{fullKind}/{namespace}/{applicationName}")
    Response updateK8sApplication(@PathVariable("fullKind") String fullKind,
                                  @PathVariable("namespace") String namespace,
                                  @PathVariable("applicationName") String applicationName,
                                  @RequestBody JSONObject applicationDetail,
                                  @RequestHeader(TokenConstants.AUTHENTICATION) String userKey);

    @PostMapping(value = "/rancher/v1/{fullKind}", consumes = {Constants.YAML_CONTENT_TYPE})
    Response createK8sApplication(@PathVariable("fullKind") String fullName,
                                  @RequestBody String yamlContent,
                                  @RequestHeader(TokenConstants.AUTHENTICATION) String userKey);

    @PostMapping(value = "/rancher/v1/management.cattle.io.clusters/{cluster_id}?action=apply")
    Response applyK8sYaml(@PathVariable("cluster_id") String cluster_id,
                          @RequestBody JSONObject yamlContent,
                          @RequestHeader(TokenConstants.AUTHENTICATION) String userKey);

    @GetMapping("/rancher/apis/apps/v1/namespaces/{namespace}/{kindName}/{applicationName}")
    Response getApplicationYamlFile(@PathVariable("kindName") String kindName,
                                    @PathVariable("namespace") String namespace,
                                    @PathVariable("applicationName") String applicationName,
                                    @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @PutMapping(value = "/rancher/v1/{fullKind}/{namespace}/{applicationName}", consumes = {Constants.YAML_CONTENT_TYPE})
    Response editK8sApplication(@PathVariable("fullKind") String fullKind,
                                @PathVariable("namespace") String namespace,
                                @PathVariable("applicationName") String applicationName,
                                @RequestBody String yamlContent,
                                @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @DeleteMapping("/rancher/v1/{fullKind}/{namespace}/{applicationName}")
    Response deleteApplication(@PathVariable("fullKind") String fullKind,
                               @PathVariable("namespace") String namespace,
                               @PathVariable("applicationName") String applicationName,
                               @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/v1/configmaps/{namespace}")
    Response getK8sConfigmapByNamespace(@PathVariable("namespace") String namespace,
                                        @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/v1/secrets/{namespace}")
    Response getK8sSecretByNamespace(@PathVariable("namespace") String namespace,
                                     @RequestHeader(TokenConstants.AUTHENTICATION) String token);

    @GetMapping("/rancher/k8s/clusters/{cluster_id}/v1/services/{namespace}")
    Response getK8sServiceByNamespace(@PathVariable("cluster_id") String cluster_id,
                                      @PathVariable("namespace") String namespace,
                                      @RequestHeader(TokenConstants.AUTHENTICATION) String token);


    @DeleteMapping("/rancher/v1/services/{serviceId}")
    Response deleteK8sService(@PathVariable("serviceId") String serviceId,@RequestHeader(TokenConstants.AUTHENTICATION) String token);
}
