package com.kospo.talk.config.exception;


import com.kospo.talk.exception.CustomException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Date;

@Slf4j
@ControllerAdvice
@Controller
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

  public static final String ANSI_RESET = "\u001B[0m";
  public static final String ANSI_YELLOW_COLOR = "\u001B[33m";


  @ExceptionHandler(CustomException.class)
  @ResponseBody
  public ResponseEntity customException(CustomException e, HandlerMethod method, HttpServletRequest request, WebRequest webRequest) {

    Date errorDate = new Date();
    String exceptionName = e.getClass().getSimpleName();
    String controllerName = method.getMethod().getDeclaringClass().getSimpleName();
    String methodName = method.getMethod().getName();
    int lineNumber = e.getStackTrace()[0].getLineNumber();
    String detail = e.getMessage();
    String requestURI = request.getRequestURI();



    log.error(ANSI_YELLOW_COLOR + "[EVENT_TIME: {} | CONTROLLER_NAME : {} | METHOD_NAME : {} | EXCEPTION_LINE : {} | EXCEPTION_NAME : {} | DETAIL : {} | REQUEST_URI : {}]" + ANSI_RESET
            , errorDate
            , controllerName
            , methodName
            , lineNumber
            , exceptionName
            , detail
            , requestURI);

    HttpHeaders resHeaders = new HttpHeaders();
    resHeaders.add("Content-Type", "application/json;charset=UTF-8");

    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).headers(resHeaders).body(
            CustomErrorResponse.builder()
                    .code(e.getErrorCode().getCode())
                    .message(e.getErrorCode().getMessage())
                    .errTime(System.currentTimeMillis()).build()
    );
  }

  @ExceptionHandler(Exception.class)
  public ResponseEntity<?> handleException(Exception e, HandlerMethod method, HttpServletRequest request, WebRequest webRequest) {

//        Date errorDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    Date errorDate = new Date();
    String exceptionName = e.getClass().getSimpleName();
    String controllerName = method.getMethod().getDeclaringClass().getSimpleName();
    String methodName = method.getMethod().getName();
    int lineNumber = e.getStackTrace()[0].getLineNumber();
    String detail = e.getMessage();
    String requestURI = request.getRequestURI();



    log.error(ANSI_YELLOW_COLOR + "[EVENT_TIME: {} | CONTROLLER_NAME : {} | METHOD_NAME : {} | EXCEPTION_LINE : {} | EXCEPTION_NAME : {} | DETAIL : {} | REQUEST_URI : {}]" + ANSI_RESET
            , errorDate
            , controllerName
            , methodName
            , lineNumber
            , exceptionName
            , detail
            , requestURI);


    HttpHeaders resHeaders = new HttpHeaders();
    resHeaders.add("Content-Type", "application/json;charset=UTF-8");
    return ResponseEntity.status(500).headers(resHeaders).body(
            CustomErrorResponse.builder()
                    .code(ErrorCode.INTERNAL_SERVER_ERROR.getCode())
                    .message(ErrorCode.INTERNAL_SERVER_ERROR.getMessage())
                    .errTime(System.currentTimeMillis()).build()
    );
  }


}
