package ru.soune.nocopy.handler;

import lombok.AllArgsConstructor;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.exception.*;

import java.util.Map;

@RestControllerAdvice
@AllArgsConstructor
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<?> handleValidationException(MethodArgumentNotValidException ex) {
        String message = ex.getBindingResult().getFieldErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .findFirst()
                .orElse("");

        return ResponseEntity.badRequest()
                .body(Map.of(
                        "success", false,
                        "message" ,message));
    }

    @ExceptionHandler(NotValidFieldException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> handleUserContainsException(NotValidFieldException ex) {

        return ResponseEntity
                .ok()
                .body(ex.getBaseResponse());
    }

    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseEntity<?> handleUserNotFoundException(UserNotFoundException ex) {
        return ResponseEntity.
                badRequest()
                .body(Map.of(
                        "success", false,
                        "message" ,ex.getMessage()
                ));
    }

    @ExceptionHandler(ContentNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseEntity<?> handleUserNotFoundException(ContentNotFoundException ex) {
        return ResponseEntity.
                badRequest()
                .body(Map.of(
                        "success", false,
                        "message" ,ex.getMessage()
                ));
    }

    @ExceptionHandler(NotValidationPasswordException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResponseEntity<?> handleNotValidationPasswordException(NotValidationPasswordException ex) {
        return ResponseEntity.
                badRequest()
                .body(Map.of(
                        "success", false,
                        "message" ,ex.getMessage()
                ));
    }

    @ExceptionHandler(TokenNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseEntity<?> handleNotFoundTokenException(TokenNotFoundException ex) {
        return ResponseEntity.
                badRequest()
                .body(Map.of(
                        "success", false,
                        "message" ,ex.getMessage()
                ));
    }

    @ExceptionHandler(DuplicateImageException.class)
    @ResponseBody
    public ResponseEntity<BaseResponse> handleDuplicateImage(DuplicateImageException e) {
        return ResponseEntity.ok().body(new BaseResponse(
                20004, MessageCode.DUPLICATE_FILE_UPLOAD.getCode(),
                "Duplicate image detected", e.getMessage()));
    }
}
