package de.htwsaar.webshop.controller;

import de.htwsaar.webshop.repository.entities.Customer;
import de.htwsaar.webshop.service.CustomerService;
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.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static de.htwsaar.webshop.config.ControllerPathConfig.CUSTOMER_BASE;
import static de.htwsaar.webshop.config.ParameterConfig.PARAM_ID;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;

@RestController
@Slf4j
public class CustomerController {

    private final ValidatorService validatorService;
    private final CustomerService customerService;

    @Autowired
    public CustomerController(ValidatorService validatorService, CustomerService customerService) {
        this.validatorService = validatorService;
        this.customerService = customerService;
    }

    @RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<Customer> getCustomerById(HttpServletRequest request,
                                                    @RequestParam(PARAM_ID) Long customerId) {
        logRequest(request);
        if (customerId == null) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().build();
        }
        Customer customer = customerService.findById(customerId);
        if (customer == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(customer);
    }

    @RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<Customer> createCustomer(HttpServletRequest request,
                                                   @RequestBody Customer customer) {
        logRequest(request);
        if (validatorService.isInvalid(customer)) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().build();
        }
        customer.setId(null);
        Customer saved = customerService.save(customer);
        if (saved.getId() == null) {
            return ResponseEntity.internalServerError().build();
        }
        return ResponseEntity.ok(saved);
    }

    @RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.DELETE, produces = "application/json")
    public ResponseEntity<Void> deleteCustomer(HttpServletRequest request,
                                               @RequestBody Customer customer) {
        logRequest(request);
        if (validatorService.isInvalid(customer)) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().build();
        }
        customerService.delete(customer);
        return ResponseEntity.ok().build();
    }

    @RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.PUT, produces = "application/json")
    public ResponseEntity<Customer> updateCustomer(HttpServletRequest request,
                                                   @RequestParam(PARAM_ID) Long id,
                                                   @RequestBody Customer customer) {
        logRequest(request);
        if (validatorService.isInvalid(customer)) {
            log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
            return ResponseEntity.badRequest().build();
        }
        if (customerService.findById(id) == null) {
            log.warn("[{}] AccountID doesn't exist", request.getRequestURI());
            return ResponseEntity.badRequest().build();
        }
        customer.setId(id);
        return ResponseEntity.ok(customerService.save(customer));
    }
}
