package com.aip.gateway.api.controller;

import com.aip.gateway.api.model.response.*;
import com.aip.gateway.api.repository.GlobalConstants;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class BaseController {

    protected String getRemoteIP(HttpServletRequest request) {
        if (request == null) {
            return "";
        }

        String ipAddr = request.getHeader("X-FORWARDED-FOR");

        // proxy 환경일 경우
        if (ipAddr == null || ipAddr.isEmpty()) {
            ipAddr = request.getHeader("Proxy-Client-IP");
        }

        // 웹로직 서버일 경우
        if (ipAddr == null || ipAddr.isEmpty()) {
            ipAddr = request.getHeader("WL-Proxy-Client-IP");
        }

        // 기타
        if (ipAddr == null || ipAddr.isEmpty()) {
            ipAddr = request.getRemoteAddr() ;
        }

        //-Djava.net.preferIPv4Stack=true
        if (ipAddr.equals("0:0:0:0:0:0:0:1"))   //==> ipv6 <== default
        {
            ipAddr = "127.0.0.1";   //==> localhost
        }
        request.setAttribute(GlobalConstants.API_IP_ADDRESS, ipAddr);
        return ipAddr;
    }

    protected ResponseEntity<ApiResponseModel> responseSuccess(GeneralResponse response) {
        return ResponseEntity.ok().body(ApiResponseModel.builder()
                .success(true)
                .statusCode(HttpStatus.OK.value())
                .message("success")
                .result(response)
                .build());
    }

    protected ResponseEntity<ApiResponseModel> responseSuccess(ResponseInfo response) {
        return ResponseEntity.ok().body(ApiResponseModel.builder()
                .success(true)
                .statusCode(HttpStatus.OK.value())
                .message("success")
                .result(response)
                .build());
    }

    protected ResponseEntity<ApiResponseModel> responseSuccess(ResponseFile response) {
        return ResponseEntity.ok().body(ApiResponseModel.builder()
                .success(true)
                .statusCode(HttpStatus.OK.value())
                .message("success")
                .result(response)
                .build());
    }

    protected  ResponseEntity<ApiResponseModel> responseSuccess(ResponseStream response) {
        return ResponseEntity.ok().body(ApiResponseModel.builder()
                .success(true)
                .statusCode(HttpStatus.OK.value())
                .message("success")
                .result(response)
                .build());
    }

    protected  ResponseEntity<ApiResponseModel> responseException(HttpServletRequest request, Exception ex) {
        request.setAttribute(GlobalConstants.API_RESULT_CODE, HttpStatus.INTERNAL_SERVER_ERROR.value());
        request.setAttribute(GlobalConstants.API_RESULT_MESSAGE, ex.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponseModel.builder()
                .success(false)
                .statusCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
                .message(ex.getMessage())
                .result(null)
                .build());
    }

    protected  ResponseEntity<ApiResponseModel> responseApiKeyValidationError(HttpServletRequest request, int error) {
        String errorMessage = "";
        if (error == 1) {
            errorMessage = "등록되어 있지 않은 API KEY 입니다.";
        }
        else if (error == 2) {
            errorMessage = "API KEY 유효기간이 지났습니다.";
        }
        else if (error == 3) {
            errorMessage = "등록되어 있지 않은 IP Address 에서 호출된 API KEY 입니다.";
        }
        else {
            errorMessage = "API KEY 유효성 검사 중 시스템 내부 오류가 발생하였습니다.";
        }
        request.setAttribute(GlobalConstants.API_RESULT_CODE, error);
        request.setAttribute(GlobalConstants.API_RESULT_MESSAGE, errorMessage);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponseModel.builder()
                .success(false)
                .statusCode(HttpStatus.BAD_REQUEST.value())
                .message(errorMessage)
                .result(null)
                .build());
    }
}
