/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hertzbeat.manager.controller;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.hertzbeat.common.entity.dto.Message;
import org.apache.hertzbeat.common.entity.job.Job;
import org.apache.hertzbeat.common.util.ResponseUtil;
import org.apache.hertzbeat.manager.pojo.dto.Hierarchy;
import org.apache.hertzbeat.manager.pojo.dto.MonitorDefineDto;
import org.apache.hertzbeat.manager.pojo.dto.ParamDefineInfo;
import org.apache.hertzbeat.manager.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Monitoring Type Management API
 */
@Tag(name = "Monitor Type Manage API")
@RestController
@RequestMapping(path = "/api/apps", produces = {APPLICATION_JSON_VALUE})
public class AppController {

    private static final String[] RISKY_STR_ARR = {"ScriptEngineManager", "URLClassLoader", "!!",
            "ClassLoader", "AnnotationConfigApplicationContext", "FileSystemXmlApplicationContext",
            "GenericXmlApplicationContext", "GenericGroovyApplicationContext", "GroovyScriptEngine",
            "GroovyClassLoader", "GroovyShell", "ScriptEngine", "ScriptEngineFactory", "XmlWebApplicationContext",
            "ClassPathXmlApplicationContext", "MarshalOutputStream", "InflaterOutputStream", "FileOutputStream"};

    @Autowired
    private AppService appService;

    @GetMapping(path = "/{app}/params")
    @Operation(summary = "The structure of the input parameters required to specify the monitoring type according to the app query",
            description = "The structure of the input parameters required to specify the monitoring type according to the app query")
    public ResponseEntity<Message<List<ParamDefineInfo>>> queryAppParamDefines(
            @Parameter(description = "en: Monitoring type name", example = "api") @PathVariable("app") final String app) {
        return ResponseUtil.handle(() -> appService.getAppParamDefines(app.toLowerCase()));
    }

    @GetMapping(path = "/{monitorId}/pushdefine")
    @Operation(summary = "The definition structure of the specified monitoring type according to the push query",
            description = "The definition structure of the specified monitoring type according to the push query")
    public ResponseEntity<Message<Job>> queryPushDefine(
            @Parameter(description = "en: Monitoring type name", example = "api") @PathVariable("monitorId") final Long monitorId) {
        return ResponseUtil.handle(() -> appService.getPushDefine(monitorId));
    }

    @GetMapping(path = "/{monitorId}/define/dynamic")
    @Operation(summary = "The definition structure of the specified monitoring type according to the push query",
            description = "The definition structure of the specified monitoring type according to the push query")
    public ResponseEntity<Message<Job>> queryAutoGenerateDynamicAppDefine(
            @Parameter(description = "Monitoring id", example = "5435345") @PathVariable("monitorId") final Long monitorId) {
        return ResponseUtil.handle(() -> appService.getAutoGenerateDynamicDefine(monitorId));
    }

    @GetMapping(path = "/{app}/define")
    @Operation(summary = "The definition structure of the specified monitoring type according to the app query",
            description = "The definition structure of the specified monitoring type according to the app query")
    public ResponseEntity<Message<Job>> queryAppDefine(
            @Parameter(description = "en: Monitoring type name", example = "api") @PathVariable("app") final String app) {
        return ResponseUtil.handle(() -> appService.getAppDefine(app.toLowerCase()));
    }

    @GetMapping(path = "/{app}/define/yml")
    @Operation(summary = "The definition yml of the specified monitoring type according to the app query",
            description = "The definition yml of the specified monitoring type according to the app query")
    public ResponseEntity<Message<String>> queryAppDefineYml(
            @Parameter(description = "en: Monitoring type name", example = "api") @PathVariable("app") final String app) {
        return ResponseUtil.handle(() -> appService.getMonitorDefineFileContent(app));
    }

    @DeleteMapping(path = "/{app}/define/yml")
    @Operation(summary = "Delete monitor define yml", description = "Delete the definition YML for the specified monitoring type according to the app")
    public ResponseEntity<Message<Void>> deleteAppDefineYml(
            @Parameter(description = "en: Monitoring type name", example = "api") @PathVariable("app") final String app) {
        return ResponseUtil.handle(() -> appService.deleteMonitorDefine(app));
    }

    @PostMapping(path = "/define/yml")
    @Operation(summary = "Add new monitoring type define yml", description = "Add new monitoring type define yml")
    public ResponseEntity<Message<Void>> newAppDefineYml(@Valid @RequestBody MonitorDefineDto defineDto) {
        return ResponseUtil.handle(() -> {
            for (String riskyToken : RISKY_STR_ARR) {
                if (defineDto.getDefine().contains(riskyToken)) {
                    throw new RuntimeException("can not has malicious remote script");
                }
            }
            appService.applyMonitorDefineYml(defineDto.getDefine(), false);
        });
    }

    @PutMapping(path = "/define/yml")
    @Operation(summary = "Update monitoring type define yml", description = "Update monitoring type define yml")
    public ResponseEntity<Message<Void>> updateAppDefineYml(@Valid @RequestBody MonitorDefineDto defineDto) {
        return ResponseUtil.handle(() -> {
            for (String riskyToken : RISKY_STR_ARR) {
                if (defineDto.getDefine().contains(riskyToken)) {
                    throw new RuntimeException("can not has malicious remote script");
                }
            }
            appService.applyMonitorDefineYml(defineDto.getDefine(), true);
        });
    }

    @GetMapping(path = "/hierarchy")
    @Operation(summary = "Query all monitor metrics level, output in a hierarchical structure", description = "Query all monitor metrics level, output in a hierarchical structure")
    public ResponseEntity<Message<List<Hierarchy>>> queryAppsHierarchy(
            @Parameter(description = "en: language type",
                    example = "zh-CN")
            @RequestParam(name = "lang", required = false) String lang) {
        String newLang = getLang(lang);
        return ResponseUtil.handle(() -> appService.getAllAppHierarchy(newLang));
    }

    @GetMapping(path = "/hierarchy/{app}")
    @Operation(summary = "Query all monitor metrics level, output in a hierarchical structure", description = "Query all monitor metrics level, output in a hierarchical structure")
    public ResponseEntity<Message<List<Hierarchy>>> queryAppsHierarchyByApp(
            @Parameter(description = "en: language type",
                    example = "zh-CN")
            @RequestParam(name = "lang", required = false) String lang,
            @Parameter(description = "en: Monitoring type name", example = "api") @PathVariable("app") final String app) {
        String newLang = getLang(lang);
        return ResponseUtil.handle(() -> appService.getAppHierarchy(app, newLang));
    }

    @GetMapping(path = "/defines")
    @Operation(summary = "Query all monitor types", description = "Query all monitor types")
    public ResponseEntity<Message<Map<String, String>>> getAllAppDefines(
            @Parameter(description = "en: language type",
                    example = "zh-CN")
            @RequestParam(name = "lang", required = false) String lang) {
        String newLang = getLang(lang);
        return ResponseUtil.handle(() -> appService.getI18nApps(newLang));
    }

    private String getLang(@RequestParam(name = "lang", required = false) @Parameter(description = "en: language type", example = "zh-CN") String lang) {
        if (lang == null || lang.isEmpty()) {
            lang = "zh-CN";
        }
        if (lang.contains(Locale.ENGLISH.getLanguage())) {
            lang = "en-US";
        } else if (lang.contains(Locale.CHINESE.getLanguage())) {
            lang = "zh-CN";
        } else {
            lang = "en-US";
        }
        return lang;
    }
}
