package com.oguerreiro.resilient.web.rest;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
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.enumeration.OrganizationNature;
import com.oguerreiro.resilient.repository.OrganizationTypeExtendedRepository;
import com.oguerreiro.resilient.service.OrganizationTypeExtendedService;
import com.oguerreiro.resilient.service.dto.OrganizationTypeDTO;
import com.oguerreiro.resilient.web.rest.errors.BadRequestAlertException;

/**
 * REST controller for managing {@link com.oguerreiro.resilient.domain.OrganizationType}.
 */
@RestController
@RequestMapping("/api/organization-types")
public class OrganizationTypeExtendedResource {

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

    private static final String ENTITY_NAME = "organizationType";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final OrganizationTypeExtendedService organizationTypeService;

    private final OrganizationTypeExtendedRepository organizationTypeRepository;

    public OrganizationTypeExtendedResource(
        OrganizationTypeExtendedService organizationTypeService,
        OrganizationTypeExtendedRepository organizationTypeRepository
    ) {
        this.organizationTypeService = organizationTypeService;
        this.organizationTypeRepository = organizationTypeRepository;
    }

    /**
     * {@code GET  /organization-types} : get all the organizationTypes.
     *
     * @param eagerload flag to eager load entities from relationships (This is applicable for many-to-many).
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of organizationTypes in body.
     */
    @GetMapping("/nature/{nature}")
    public List<OrganizationTypeDTO> getOrganizationTypesForNature (
        @PathVariable("nature") String nature
    ) {
    	log.debug("REST request to get all OrganizationTypes");
    	
    	OrganizationNature parentNature = OrganizationNature.valueOf(nature);
        
    	if (parentNature == null) {
    		//Invalid request. Nature unknown.
    		throw new BadRequestAlertException("Nature not found", ENTITY_NAME, "idnotfound");
    	}
    	
    	List<OrganizationNature> natures = new ArrayList<OrganizationNature>();
    	natures.add(OrganizationNature.PERSON);
    	natures.add(OrganizationNature.LEVEL);
    	if (parentNature.equals(OrganizationNature.ORGANIZATION) || parentNature.equals(OrganizationNature.LEVEL)) natures.add(OrganizationNature.ORGANIZATION);
    	if (parentNature.equals(OrganizationNature.ORGANIZATION) || parentNature.equals(OrganizationNature.LEVEL)) natures.add(OrganizationNature.FACILITY);
    	
        return organizationTypeService.findByNatureIn(natures);
    }
}
