package com.oguerreiro.resilient.web.rest;

import java.util.List;

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.Unit;
import com.oguerreiro.resilient.repository.UnitRepository;
import com.oguerreiro.resilient.service.UnitService;
import com.oguerreiro.resilient.service.criteria.UnitCriteria;
import com.oguerreiro.resilient.service.dto.UnitDTO;
import com.oguerreiro.resilient.service.query.UnitQueryService;

/**
 * REST controller for managing {@link com.oguerreiro.resilient.domain.Unit}.
 */
@RestController
@RequestMapping("/api/units")
public class UnitResource extends AbstractResilientQueryResource<Unit, UnitDTO, Long, UnitCriteria> {

  private static final String ENTITY_NAME = "unit";

  private final UnitService unitService;

  private final UnitRepository unitRepository;
  private final UnitQueryService unitQueryService;

  public UnitResource(UnitService unitService, UnitRepository unitRepository, UnitQueryService unitQueryService) {
    super(Unit.class, unitRepository, unitService, unitQueryService);
    this.unitService = unitService;
    this.unitRepository = unitRepository;
    this.unitQueryService = unitQueryService;
  }

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

  /* UnitResource endpoints */

  /**
   * {@code GET  /units/byType/:unitTypeId} : get all the units.
   *
   * @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 units in body.
   */
  @GetMapping("/byType/{unitTypeId}")
  public List<UnitDTO> getAllUnitsByType(@PathVariable("unitTypeId") Long unitTypeId) {
    log.debug("REST request to get all UnitsByType Id : {}", unitTypeId);
    return unitService.findAllByType(unitTypeId);
  }

  /**
   * {@code GET  /units/forVariable/:variableId} : get all the units allowed in variable.
   *
   * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of units in body.
   */
  @GetMapping("/forVariable/{variableId}")
  public List<UnitDTO> getAllUnitsAllowedForVariable(@PathVariable("variableId") Long variableId) {
    log.debug("REST request to get all getAllUnitsAllowedForVariable Id : {}", variableId);
    return unitService.findAllUnitsAllowedForVariable(variableId);
  }
}
