package com.oguerreiro.resilient.web.rest;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oguerreiro.resilient.domain.Organization;
import com.oguerreiro.resilient.domain.enumeration.OrganizationNature;
import com.oguerreiro.resilient.repository.OrganizationRepository;
import com.oguerreiro.resilient.service.OrganizationService;
import com.oguerreiro.resilient.service.dto.OrganizationDTO;

/**
 * REST controller for managing {@link com.oguerreiro.resilient.domain.Organization}.
 */
@RestController
@RequestMapping("/api/organizations")
public class OrganizationResource extends AbstractResilientResource<Organization, OrganizationDTO, Long> {

  private static final String ENTITY_NAME = "organization";
  private final OrganizationService organizationService;

  public OrganizationResource(OrganizationService organizationService, OrganizationRepository organizationRepository) {
    super(Organization.class, organizationRepository, organizationService);
    this.organizationService = organizationService;
  }

  /* Abstract methods impl*/
  @Override
  protected String getEntityName() {
    return ENTITY_NAME;
  }

  /* OrganizationResource endpoints */
  /**
   * {@code GET /organizations/byNature/{nature} : get all the Organization within the provided nature.
   *
   * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of Organizations in body.
   */
  @PreAuthorize("@resilientSecurity.canAccess('READ')") // Must have READ permission
  @GetMapping("/byNature/{nature}")
  public List<OrganizationDTO> getAllByNature(@PathVariable("nature") OrganizationNature nature) {
    log.debug("REST request to get all getAllByNature : {}", nature);
    return organizationService.findAllByNature(nature, null, null);
  }

  /**
   * {@code GET /organizations/byNature/forInput/{nature} : get all the Organization within the provided nature.
   *
   * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of Organizations in body.
   */
  @PreAuthorize("@resilientSecurity.canAccess('READ')") // Must have READ permission
  @GetMapping("/byNature/forInput/{nature}")
  public List<OrganizationDTO> getAllByNatureForInput(@PathVariable("nature") OrganizationNature nature) {
    log.debug("REST request to get all getAllByNature : {}", nature);
    return organizationService.findAllByNature(nature, Boolean.TRUE, null);
  }

  /**
   * {@code GET /organizations/hierarchy/{nature} : get all the Organization within the provided nature, that the user
   * has access (create/update) to.
   * @param nature
   * 
   * @return
   */
  @PreAuthorize("@resilientSecurity.canAccess('READ')") // Must have READ permission
  @GetMapping("/hierarchy/{nature}")
  public List<OrganizationDTO> getAllByNatureForUserHierarchy(@PathVariable("nature") OrganizationNature nature) {
    log.debug("REST request to get all getAllByNatureForUserHierarchy : {}", nature);

    return organizationService.findAllByNatureForUserHierarchy(nature, null, null);
  }

  /**
   * {@code GET /organizations/hierarchy/{nature} : get all the Organization within the provided nature, that the user
   * has access (create/update) to.
   * @param nature
   * 
   * @return
   */
  @PreAuthorize("@resilientSecurity.canAccess('READ')") // Must have READ permission
  @GetMapping("/hierarchy/forInput/{nature}")
  public List<OrganizationDTO> getAllByNatureForUserHierarchyForInput(
      @PathVariable("nature") OrganizationNature nature) {
    log.debug("REST request to get all getAllByNatureForUserHierarchy : {}", nature);

    return organizationService.findAllByNatureForUserHierarchy(nature, Boolean.TRUE, null);
  }
}
