package org.egl_cepgl.pm.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.egl_cepgl.pm.dto.UserDto;
import org.egl_cepgl.pm.model.user.User;
import org.egl_cepgl.pm.repository.UserRepository;
import org.egl_cepgl.pm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api("/api/users")
@RestController
@RequestMapping("/api/users")
public class UserController
{
    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    //@PreAuthorize("hasAuthority('ROLE_default-roles-egl_pm')")
    @PostMapping(value = "/addIfNotExists",consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="Add ..", notes="Cette méthode permet de..", response = User.class)
    @ApiResponses(value= { @ApiResponse(code= 200, message = "créé/modifié"), @ApiResponse(code= 400, message = "Non valide") })
    public ResponseEntity<UserDto> addIfNotExists(@RequestBody UserDto userDto)
    {
        //log.info("BVDDDDDDDDDDDDEEEE==="+userDto.toString());
        return ResponseEntity.ok(this.userService.addUser(userDto));
    }

    @GetMapping(value = "/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="List ..", notes="Cette méthode permet..", responseContainer = "List<User>")
    @ApiResponses(value= { @ApiResponse(code= 200, message = "Une liste des ..") })
    public ResponseEntity<List<User>> findAll()
    {
        return ResponseEntity.ok(userService.findAllUsers());
    }

    //@PreAuthorize("hasAuthority('ROLE_admin')")
    @GetMapping(value = "/findByEmail",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<UserDto> findByEmail(String user, Boolean create_if_not_exists) throws JsonProcessingException { return ResponseEntity.ok(userService.findByEmail(user, create_if_not_exists)); }
    
}
