package com.aip.gateway.api.controller;

import com.aip.gateway.api.model.request.RequestFile;
import com.aip.gateway.api.model.request.RequestFileAllSet;
import com.aip.gateway.api.model.request.RequestFileDel;
import com.aip.gateway.api.model.request.RequestFileSet;
import com.aip.gateway.api.model.response.ApiResponseModel;
import com.aip.gateway.api.repository.AppRepository;
import com.aip.gateway.api.repository.GlobalConstants;
import com.aip.gateway.api.service.IApiFileService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "ApiFile", description = "FILE AIP API")
@RestController
@RequiredArgsConstructor
@CrossOrigin
@Validated
@RequestMapping("/v1/aip-api/file")
public class ApiFileController extends  BaseController {

    private final AppRepository appRepo;
    private final IApiFileService service;

    @Operation(summary = "File Info", description = "AIP File Information Query.")
    @ApiResponse(responseCode = "200", description = "AIP File Information query Success.")
    @ApiOperation(value = "File Info", response = ApiResponseModel.class)
    @PostMapping(value = "info", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> getInfo(
            @Parameter(name = "req", description = "File 정보 요청 데이터", required = true)
            @RequestBody @Valid final RequestFile req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_INFO, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
            if (authError != 0) {
                return responseApiKeyValidationError(request, authError);
            }
            return responseSuccess(this.service.getInfo(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/info, {}", e.getMessage());
            return responseException(request, e);
        }
    }

    @Operation(summary = "File AIP Label 설정", description = "File AIP Label Set.")
    @ApiResponse(responseCode = "200", description = "File AIP Label set success.")
    @ApiOperation(value = "File AIP Label 설정", response = ApiResponseModel.class, responseContainer = "ArrayList")
    @PostMapping(value = "set-label", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> setLabel(
            @Parameter(name = "req", description = "File AIP Label 설정 요청 데이터", required = true)
            @RequestBody @Valid final RequestFileSet req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_SET_LABEL, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
            if (authError != 0) {
                return responseApiKeyValidationError(request, authError);
            }
            return responseSuccess(this.service.setLabel(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/set-label, {}", e.getMessage());
            return responseException(request, e);
        }
    }

    @Operation(summary = "File AIP Label Delete", description = "File AIP Label Delete.")
    @ApiResponse(responseCode = "200", description = "File AIP Label delete success.")
    @ApiOperation(value = "File AIP Label 해제", response = ApiResponseModel.class, responseContainer = "ArrayList")
    @PostMapping(value = "delete-label", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> delLabel(
            @Parameter(name = "req", description = "File AIP Label 해제 요청 데이터", required = true)
            @RequestBody @Valid final RequestFileDel req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_DELETE_LABEL, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
            if (authError != 0) {
                return responseApiKeyValidationError(request, authError);
            }
            return responseSuccess(this.service.delLabel(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/delete-label, {}", e.getMessage());
            return responseException(request, e);
        }
    }

    @Operation(summary = "File AIP Protection 설정", description = "File AIP Protection AIP.")
    @ApiResponse(responseCode = "200", description = "File AIP Protection set success.")
    @ApiOperation(value = "File AIP Protection 설정", response = ApiResponseModel.class, responseContainer = "ArrayList")
    @PostMapping(value = "set-protection", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> setProtection(
            @Parameter(name = "req", description = "File AIP Protection 설정 요청 데이터", required = true)
            @RequestBody @Valid final RequestFileSet req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_SET_PROTECTION, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
             if (authError != 0) {
                 return responseApiKeyValidationError(request, authError);
             }
             return responseSuccess(this.service.setProtection(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/set-protection, {}", e.getMessage());
            return responseException(request, e);
        }
    }

    @Operation(summary = "File AIP Protection Delete", description = "File AIP Protection Delete.")
    @ApiResponse(responseCode = "200", description = "File AIP Protection delete success.")
    @ApiOperation(value = "File AIP Protection 해제", response = ApiResponseModel.class, responseContainer = "ArrayList")
    @PostMapping(value = "delete-protection", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> delProtection(
            @Parameter(name = "req", description = "File AIP Protection 해제 요청 데이터", required = true)
            @RequestBody @Valid final RequestFileDel req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_DELETE_PROTECTIN, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
            if (authError != 0) {
                return responseApiKeyValidationError(request, authError);
            }
            return responseSuccess(this.service.delProtection(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/delete-protection, {}", e.getMessage());
            return responseException(request, e);
        }
    }

    @Operation(summary = "File AIP Label Or Protection 설정", description = "File AIP Label Or Protection AIP.")
    @ApiResponse(responseCode = "200", description = "File AIP Label Or Protection set success.")
    @ApiOperation(value = "File AIP Label Or Protection 설정", response = ApiResponseModel.class, responseContainer = "ArrayList")
    @PostMapping(value = "set-label-protection", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> setLabelProtection(
            @Parameter(name = "req", description = "File AIP Label Or Protection 설정 요청 데이터", required = true)
            @RequestBody @Valid final RequestFileAllSet req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_SET_LABEL_PROTECTION, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
            if (authError != 0) {
                return responseApiKeyValidationError(request, authError);
            }
            return responseSuccess(this.service.setLabelProtection(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/set-label-protection, {}", e.getMessage());
            return responseException(request, e);
        }
    }

    @Operation(summary = "File AIP Label Or Protection Delete", description = "File AIP Label Or Protection Delete.")
    @ApiResponse(responseCode = "200", description = "File AIP Label Or Protection delete success.")
    @ApiOperation(value = "File AIP Label Or Protection 해제", response = ApiResponseModel.class, responseContainer = "ArrayList")
    @PostMapping(value = "delete-label-protection", produces = {"application/json; charset=utf8"})
    public ResponseEntity<ApiResponseModel> delLabelProtection(
            @Parameter(name = "req", description = "File AIP Label Or Protection 해제 요청 데이터", required = true)
            @RequestBody @Valid final RequestFileDel req, HttpServletRequest request)
    {
        try {
            GlobalConstants.setAuthorization(request, GlobalConstants.API_FILE_DELETE_LABEL_PROTECTION, req.getApiKey(), req);
            int authError = this.appRepo.checkApiKeyValidation(req.getApiKey(), getRemoteIP(request));
            if (authError != 0) {
                return responseApiKeyValidationError(request, authError);
            }
            return responseSuccess(this.service.delLabelProtection(req));
        }
        catch (Exception e) {
            log.error("exception: /v1/aip-api/file/delete-label-protection, {}", e.getMessage());
            return responseException(request, e);
        }
    }

}
