package de.htwsaar.webshop.controller;

import de.htwsaar.webshop.repository.entities.Order;
import de.htwsaar.webshop.service.OrderService;
import de.htwsaar.webshop.service.ValidatorService;
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.List;

import static de.htwsaar.webshop.config.ControllerPathConfig.ORDER_BASE;
import static de.htwsaar.webshop.config.ControllerPathConfig.ORDER_GET_ALL;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;

@RestController
@Slf4j
public class OrderController {

    private final OrderService orderService;
    private final ValidatorService validatorService;

    @Autowired
    public OrderController(OrderService orderService, ValidatorService validatorService) {
        this.orderService = orderService;
        this.validatorService = validatorService;
    }

    @RequestMapping(path = ORDER_GET_ALL, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<List<Order>> getAll(HttpServletRequest request,
                                                   @RequestParam(value = PARAM_CUSTOMER_ID) Long customerId) {
        logRequest(request);
        List<Order> orders = orderService.getAllOrders(customerId);
        if(orders.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(orders);
    }

    @RequestMapping(path = ORDER_BASE, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<Order> get(HttpServletRequest request,
                                               @RequestParam(value = PARAM_ID) Long orderId) {
        logRequest(request);
        Order image = orderService.getOrderById(orderId);
        if(image == null) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(image);
    }

    @RequestMapping(path = ORDER_BASE, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<Boolean> add(HttpServletRequest request,
                                       @RequestBody Order order) {
        logRequest(request);

        if (validatorService.isInvalid(order)) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().body(false);
        }

        Order saved = orderService.save(order);
        return ResponseEntity.ok(saved != null);
    }

    //TODO: check whether an order should be updatable
    @RequestMapping(path = ORDER_BASE, method = RequestMethod.PUT, produces = "application/json")
    public ResponseEntity<Boolean> update(HttpServletRequest request,
                                          @RequestParam(value = PARAM_ID) Long orderId,
                                          @RequestBody Order order) {
        logRequest(request);
        if (orderId == null || orderService.getOrderById(orderId) == null) {
            return ResponseEntity.badRequest().body(false);
        }
        order.setId(orderService.getOrderById(orderId).getId());
        return ResponseEntity.ok(orderService.save(order) != null);
    }

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