package edu.prlab.tyler.iotgateway.gateway.controllers;

import edu.prlab.tyler.iotgateway.gateway.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.gateway.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/user")
    public ResponseEntity<SensitiveUser> register(@RequestBody SensitiveUser user) {
        return userService.registerNewUser(user)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }

    @PostMapping("/login")
    public ResponseEntity<SensitiveUser> login(@RequestBody SensitiveUser user) {
        return userService.login(user)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.noContent().build());
    }
}
