package com.mr.controller;

import com.mr.model.User;
import com.mr.service.UserService;
import com.mr.util.CustomErrorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class UserApiController {

    private static final Logger logger = LoggerFactory.getLogger(UserApiController.class);

    @Autowired
    private UserService userService; //Service which will do all data retrieval/manipulation work

    // -------------------Retrieve All Users---------------------------------------------

    @GetMapping("/users")
    public ResponseEntity<List<User>> listAllUsers() {
        List<User> users = userService.findAllUsers();
        if (users.isEmpty()) {
            users = new ArrayList<>();
            //return new ResponseEntity(HttpStatus.NO_CONTENT);
            // You many decide to return HttpStatus.NOT_FOUND
        }
        return new ResponseEntity<>(users, HttpStatus.OK);
    }

    // -------------------Retrieve Single User------------------------------------------

    @GetMapping("/users/{id}")
    public ResponseEntity<?> getUser(@PathVariable Long id) {
        logger.info("Fetching User with id {}", id);
        User user = userService.findById(id);
        if (user == null) {
            logger.error("User with id {} not found.", id);
            return new ResponseEntity(new CustomErrorType(String.format("User with id %d not found", id)), HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    // -------------------Create a User-------------------------------------------

    @PostMapping("/users")
    public ResponseEntity<?> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
        logger.info("Creating User : {}", user);

        if (userService.isUserExist(user)) {
            logger.error("Unable to create. A User with name {} already exist", user.getName());
            return new ResponseEntity(new CustomErrorType(String.format("Unable to create. A User with name %s already exist.", user.getName())), HttpStatus.CONFLICT);
        }
        userService.saveUser(user);

        /*HttpHeaders headers = new HttpHeaders();
        headers.setLocation(ucBuilder.path("/api/user/{id}").buildAndExpand(user.getId()).toUri());
        return new ResponseEntity<String>(headers, HttpStatus.CREATED);*/
        
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }

    // ------------------- Update a User ------------------------------------------------

    @PutMapping("/users/{id}")
    public ResponseEntity<?> updateUser(@PathVariable Long id, @RequestBody User user) {
        logger.info("Updating User with id {}", id);

        User currentUser = userService.findById(id);

        if (currentUser == null) {
            logger.error("Unable to update. User with id {} not found.", id);
            return new ResponseEntity(new CustomErrorType(String.format("Unable to upate. User with id  %d not found.", id)), HttpStatus.NOT_FOUND);
        }

        currentUser.setName(user.getName());
        currentUser.setAge(user.getAge());
        currentUser.setSalary(user.getSalary());

        userService.updateUser(currentUser);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    // ------------------- Delete a User-----------------------------------------

    @DeleteMapping("/users/{id}")
    public ResponseEntity<?> deleteUser(@PathVariable Long id) {
        logger.info("Fetching & Deleting User with id {}", id);

        User user = userService.findById(id);
        if (user == null) {
            logger.error("Unable to delete. User with id {} not found.", id);
            return new ResponseEntity(new CustomErrorType(String.format("Unable to delete. User with id  %d not found.", id)), HttpStatus.NOT_FOUND);
        }
        userService.deleteUserById(id);
        return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
    }

    // ------------------- Delete All Users-----------------------------

    @DeleteMapping("/users")
    public ResponseEntity<User> deleteAllUsers() {
        logger.info("Deleting All Users");

        userService.deleteAllUsers();
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}
@@ -823,99 +823,8 @@
 rk%0A%0A
-    // -------------------Retrieve All Users---------------------------------------------%0A%0A
     
@@ -1037,290 +1037,74 @@
     
-    //return new ResponseEntity(HttpStatus.NO_CONTENT);%0A            // You many decide to return HttpStatus.NOT_FOUND%0A        %7D%0A        return new ResponseEntity%3C%3E(users, HttpStatus.OK);%0A    %7D%0A%0A    // -------------------Retrieve Single User------------------------------------------
+%7D%0A        return new ResponseEntity%3C%3E(users, HttpStatus.OK);%0A    %7D
 %0A%0A  
@@ -1590,92 +1590,8 @@
  %7D%0A%0A
-    // -------------------Create a User-------------------------------------------%0A%0A
     
@@ -2426,99 +2426,8 @@
  %7D%0A%0A
-    // ------------------- Update a User ------------------------------------------------%0A%0A
     
@@ -3177,91 +3177,8 @@
  %7D%0A%0A
-    // ------------------- Delete a User-----------------------------------------%0A%0A
     
@@ -3764,82 +3764,8 @@
  %7D%0A%0A
-    // ------------------- Delete All Users-----------------------------%0A%0A
     

remove unused code