package de.htwsaar.webshop.controller;

import de.htwsaar.webshop.model.FarmImageModel;
import de.htwsaar.webshop.repository.entities.FarmImage;
import de.htwsaar.webshop.service.ArticleService;
import de.htwsaar.webshop.service.FarmImageService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

import static de.htwsaar.webshop.config.ControllerPathConfig.FARM_IMAGE_BASE;
import static de.htwsaar.webshop.config.ControllerPathConfig.FARM_IMAGE_DEFAULT;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;

@RestController
@Slf4j
public class FarmImageController {
    private final FarmImageService farmImageService;
    private final ArticleService articleService;

    @Autowired
    public FarmImageController(FarmImageService imageService, ArticleService articleService) {
        this.farmImageService = imageService;
        this.articleService = articleService;
    }

    @RequestMapping(path = FARM_IMAGE_DEFAULT, method = RequestMethod.GET, produces = "text/plain")
    public ResponseEntity<String> getDefault(HttpServletRequest request) {
        logRequest(request);
        FarmImage image = farmImageService.getStandardImage();
        if (image == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(image.getBase64());
    }

    @RequestMapping(path = FARM_IMAGE_BASE, method = RequestMethod.GET, produces = "text/plain")
    public ResponseEntity<String> getFirst(HttpServletRequest request,
                                           @RequestParam(value = PARAM_UUID) UUID uuid) {
        logRequest(request);
        FarmImage image = farmImageService.getImageByUUID(uuid);
        if (image == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(image.getBase64());
    }

    @RequestMapping(path = FARM_IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<Boolean> add(HttpServletRequest request,
                                       @RequestParam(value = PARAM_UUID) UUID articleUuid,
                                       @RequestParam(value = PARAM_STANDARD) boolean standard,
                                       @RequestBody String base64) {
        logRequest(request);

        if (articleUuid == null || articleService.findByUUID(articleUuid) == null
                || base64 == null || base64.isEmpty()) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }

        FarmImage a = farmImageService.save(articleUuid, base64, standard);
        return ResponseEntity.ok(a != null);
    }

    @RequestMapping(path = FARM_IMAGE_BASE, method = RequestMethod.PUT, produces = "application/json")
    public ResponseEntity<Boolean> update(HttpServletRequest request,
                                          @RequestParam(value = PARAM_ID) Long imageId,
                                          @RequestBody FarmImageModel image) {
        logRequest(request);
        if (imageId == null || farmImageService.getImageById(imageId) == null) {
            return ResponseEntity.badRequest().body(false);
        }
        image.setId(farmImageService.getImageById(imageId).getId());
        return ResponseEntity.ok(farmImageService.save(image) != null);
    }

    @RequestMapping(path = FARM_IMAGE_BASE, method = RequestMethod.DELETE, produces = "application/json")
    public ResponseEntity<Boolean> delete(HttpServletRequest request,
                                          @RequestParam(value = PARAM_ID) Long imageId) {
        logRequest(request);
        if (imageId == null) {
            log.warn("[{}] got invalid imageId", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }
        if (farmImageService.getImageById(imageId) != null) {
            log.warn("[{}] got invalid imageId", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }
        farmImageService.deleteById(imageId);
        return ResponseEntity.ok().build();
    }
}
