package za.co.rootbranch.coffeechat.controllers;

import java.util.List;

import org.hibernate.service.spi.InjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import za.co.rootbranch.coffeechat.models.User;
import za.co.rootbranch.coffeechat.services.UserService;
@CrossOrigin("*")
@RestController
public class UsersController {

    @Autowired
    private UserService service;

    @PostMapping("/api/user")
    public ResponseEntity<String> CreateUser(@RequestBody User user) {
        System.out.println("Create new user " + user.getEmail());
        try {
            service.createUser(user);
        } catch (Exception e) {
            return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<String>("OK", HttpStatus.CREATED);
    }

    @GetMapping("/api/user/me")
    public User GetUserProfile(@RequestParam String email) {
        User found = service.findUserByEmail(email);
        return found;
    }

    @GetMapping("/api/users")
    public List<User> listUsers() {
        return service.listUsers();
    }
}
