package com.oguerreiro.resilient.web.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
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.InputDataUpload;
import com.oguerreiro.resilient.domain.Organization;
import com.oguerreiro.resilient.domain.User;
import com.oguerreiro.resilient.repository.InputDataUploadRepository;
import com.oguerreiro.resilient.repository.UserRepository;
import com.oguerreiro.resilient.security.SecurityUtils;
import com.oguerreiro.resilient.service.InputDataUploadService;
import com.oguerreiro.resilient.service.OrganizationService;
import com.oguerreiro.resilient.service.dto.InputDataUploadDTO;

import tech.jhipster.web.util.HeaderUtil;

/**
 * REST controller for managing {@link com.oguerreiro.resilient.domain.InputDataUpload}.
 */
@RestController
@RequestMapping("/api/input-data-uploads")
public class InputDataUploadResource extends AbstractResilientResource<InputDataUpload, InputDataUploadDTO, Long> {

  private static final String ENTITY_NAME = "inputDataUpload";

  private final InputDataUploadService inputDataUploadService;
  private final OrganizationService organizationService;
  private final UserRepository userRepository;

  private final InputDataUploadRepository inputDataUploadRepository;

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

  public InputDataUploadResource(InputDataUploadService inputDataUploadService,
      InputDataUploadRepository inputDataUploadRepository, OrganizationService organizationService,
      UserRepository userRepository) {
    super(InputDataUpload.class, inputDataUploadRepository, inputDataUploadService);
    this.inputDataUploadService = inputDataUploadService;
    this.inputDataUploadRepository = inputDataUploadRepository;
    this.organizationService = organizationService;
    this.userRepository = userRepository;
  }

  /* Overriden methods */
  @Override
  @PreAuthorize("@resilientSecurity.canAccess()")
  @GetMapping("")
  public List<InputDataUploadDTO> getAll() {
    log.debug("REST request to get all " + this.getClass().getSimpleName() + "'s.");

    // Calculate the list of Organizations that it can see
    //Get current authenticated user
    User currentUser = SecurityUtils.getCurrentUserLogin().flatMap(this.userRepository::findOneByLogin).orElseThrow();

    List<Organization> organizations = organizationService.getOrganizationsForUserName(currentUser);

    return this.inputDataUploadService.findAllOwned(organizations);
  }

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

  /**
   * {@code GET  /input-data-uploads} : get all the inputDataUploads.
   *
   * @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 inputDataUploads in body.
   */
  @GetMapping("/{id}/doLoad")
  public ResponseEntity<Void> doLoad(@PathVariable("id") Long id) {
    log.debug("REST request to doload() for InputDataUpload id = " + id);

    // Execute load
    inputDataUploadService.doLoad(id);

    return ResponseEntity.noContent().headers(
        HeaderUtil.createAlert(applicationName, "Import has started ...", id.toString())).build();
  }
}
