package de.htwsaar.webshop.controller;

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

import java.util.List;
import java.util.UUID;

import static de.htwsaar.webshop.config.ControllerPathConfig.ARTICLE_BASE;
import static de.htwsaar.webshop.config.ControllerPathConfig.ARTICLE_GET_ALL;
import static de.htwsaar.webshop.config.ParameterConfig.PARAM_UUID;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;

@RestController
@Slf4j
public class ArticleController {
    private final ArticleService articleService;

    @Autowired
    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    @RequestMapping(path = ARTICLE_GET_ALL, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<ArticleModel>> getAll(HttpServletRequest request) {
        logRequest(request);
        return ResponseEntity.ok(articleService.from(articleService.findAll()));
    }

    @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<ArticleModel> getByUUID(HttpServletRequest request,
                                                  @RequestParam(value = PARAM_UUID) UUID uuid) {
        logRequest(request);
        return ResponseEntity.ok(articleService.from(articleService.findByUUID(uuid)));
    }

    @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<Boolean> add(HttpServletRequest request,
                                       @RequestBody Article article) {
        logRequest(request);
        if (article.getPrice100() < 0 || article.getStock() < 0 ||
                article.getDiscount100() > 100 || article.getDiscount100() < 0) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }

        Article a = articleService.save(article);
        return ResponseEntity.ok(a != null);
    }

    @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.PUT, produces = "application/json")
    public ResponseEntity<Boolean> update(HttpServletRequest request,
                                          @RequestParam(value = PARAM_UUID) UUID uuid,
                                          @RequestBody Article article) {
        logRequest(request);
        if (uuid == null || uuid.toString().isEmpty() || articleService.findByUUID(uuid) == null) {
            return ResponseEntity.badRequest().body(false);
        }
        article.setId(articleService.findByUUID(uuid).getId());

        return ResponseEntity.ok(articleService.save(article) != null);
    }

    @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.DELETE, produces = "application/json")
    public ResponseEntity<Boolean> delete(HttpServletRequest request,
                                          @RequestParam(value = PARAM_UUID) UUID uuid) {
        logRequest(request);
        if (uuid == null || uuid.toString().isEmpty() || articleService.findByUUID(uuid) == null) {
            log.warn("[{}] got invalid UUID {}", request.getRequestURI(), uuid);
            return ResponseEntity.badRequest().body(false);
        }
        articleService.delete(uuid);

        return ResponseEntity.ok().build();
    }
}
