/*
 * Copyright (c) 2021 LG Electronics Inc.
 * SPDX-License-Identifier: AGPL-3.0-only 
 */

package oss.fosslight.api.controller.v1;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import oss.fosslight.CoTopComponent;
import oss.fosslight.api.entity.CommonResult;
import oss.fosslight.api.service.ResponseService;
import oss.fosslight.common.CoCodeManager;
import oss.fosslight.common.CoConstDef;
import oss.fosslight.common.Url.API;
import oss.fosslight.service.ApiVulnerabilityService;
import oss.fosslight.service.T2UserService;

@Api(tags = {"4. Vulnerability"})
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/api/v1")
public class ApiVulnerabilityController extends CoTopComponent {
	
	/** The response service. */
	private final ResponseService responseService;
	
	/** The user service. */
	private final T2UserService userService;
	
	private final ApiVulnerabilityService apiVulnerabilityService;
	
	
	@ApiOperation(value = "Search Vulnerability Info", notes = "NVD Data 조회")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "_token", value = "token", required = true, dataType = "String", paramType = "header")
    })
	@GetMapping(value = {API.FOSSLIGHT_API_VULNERABILITY_DATA})
    public CommonResult getVulnerabilityData(
    		@RequestHeader String _token,
    		@ApiParam(value = "OSS name", required = false) @RequestParam(required = false) String ossName,
    		@ApiParam(value = "OSS version", required = false) @RequestParam(required = false) String ossVersion,
    		@ApiParam(value = "CVE_ID", required = false) @RequestParam(required = false) String cveId) {
		
		// 사용자 인증
		userService.checkApiUserAuth(_token);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			// ossName과 cveId 둘 다 입력하지 않을 경우 에러 처리
			if (isEmpty(ossName) && isEmpty(cveId)) {
				return responseService.getFailResult(CoConstDef.CD_OPEN_API_PARAMETER_ERROR_MESSAGE
					, CoCodeManager.getCodeString(CoConstDef.CD_OPEN_API_MESSAGE, CoConstDef.CD_OPEN_API_PARAMETER_ERROR_MESSAGE));
			}

			if (isEmpty(ossVersion)) { // ossVersion을 입력하지 않을 경우 전체 검색으로 처리함.
				ossVersion = "-";
			}
			
			List<Map<String, Object>> content = apiVulnerabilityService.selectNvdList(ossName, ossVersion, cveId);
			
			if (content.size() > 0) {
				resultMap.put("content", content);
			}
			
			return responseService.getSingleResult(resultMap);
		} catch (Exception e) {
			return responseService.getFailResult(CoConstDef.CD_OPEN_API_UNKNOWN_ERROR_MESSAGE
					, CoCodeManager.getCodeString(CoConstDef.CD_OPEN_API_MESSAGE, CoConstDef.CD_OPEN_API_UNKNOWN_ERROR_MESSAGE));
		}
    }
	
	@ApiOperation(value = "Search Vulnerability Max Score Info", notes = "NVD Max Score Data 조회")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "_token", value = "token", required = true, dataType = "String", paramType = "header")
    })
	@GetMapping(value = {API.FOSSLIGHT_API_VULNERABILITY_MAX_DATA})
    public CommonResult getVulnerabilityMaxData(
    		@RequestHeader String _token,
    		@ApiParam(value = "OSS name", required = true) @RequestParam(required = true) String ossName,
    		@ApiParam(value = "OSS version", required = false) @RequestParam(required = false) String ossVersion) {
		
		// 사용자 인증
		userService.checkApiUserAuth(_token);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			if (isEmpty(ossVersion)) { // ossVersion을 입력하지 않을 경우 전체 검색으로 처리함.
				ossVersion = "-";
			}
			
			List<Map<String, Object>> content = apiVulnerabilityService.selectMaxScoreNvdInfo(ossName, ossVersion);
			
			if (content.size() > 0) {
				resultMap.put("content", content);
			}	
			
			return responseService.getSingleResult(resultMap);
			
		} catch (Exception e) {
			return responseService.getFailResult(CoConstDef.CD_OPEN_API_UNKNOWN_ERROR_MESSAGE
					, CoCodeManager.getCodeString(CoConstDef.CD_OPEN_API_MESSAGE, CoConstDef.CD_OPEN_API_UNKNOWN_ERROR_MESSAGE));
		}
    }
}
