package com.oguerreiro.resilient.web.rest;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.oguerreiro.resilient.service.UserService;
import com.oguerreiro.resilient.service.dto.UserDTO;

import tech.jhipster.web.util.PaginationUtil;

@RestController
@RequestMapping("/api")
public class PublicUserResource {

  private static final List<String> ALLOWED_ORDERED_PROPERTIES = Collections.unmodifiableList(
      Arrays.asList("id", "login", "firstName", "lastName", "email", "activated", "langKey"));

  private final Logger log = LoggerFactory.getLogger(PublicUserResource.class);

  private final UserService userService;

  public PublicUserResource(UserService userService) {
    this.userService = userService;
  }

  /**
   * {@code GET /users} : get all users with only public information - calling this method is allowed for anyone.
   *
   * @param  pageable the pagination information.
   * @return          the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users.
   */
  @GetMapping("/users")
  public ResponseEntity<List<UserDTO>> getAllPublicUsers(
      @org.springdoc.core.annotations.ParameterObject Pageable pageable) {
    log.debug("REST request to get all public User names");
    if (!onlyContainsAllowedProperties(pageable)) {
      return ResponseEntity.badRequest().build();
    }

    final Page<UserDTO> page = userService.getAllPublicUsers(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(),
        page);
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
  }

  /**
   * {@code GET /users} : get all users with only public information - calling this method is allowed for anyone.
   *
   * @param  pageable the pagination information.
   * @return          the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users.
   */
  @GetMapping("/users/for/organization")
  public ResponseEntity<List<UserDTO>> getAllPublicUsersForOrganization(
      @org.springdoc.core.annotations.ParameterObject Pageable pageable) {
    log.debug("REST request to get all public User names, restricted to users not yet associated in the organization");
    if (!onlyContainsAllowedProperties(pageable)) {
      return ResponseEntity.badRequest().build();
    }

    final Page<UserDTO> page = userService.getAllPublicUsersForOrganization(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(),
        page);
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
  }

  private boolean onlyContainsAllowedProperties(Pageable pageable) {
    return pageable.getSort().stream().map(Sort.Order::getProperty).allMatch(ALLOWED_ORDERED_PROPERTIES::contains);
  }
}
