package com.oguerreiro.resilient.web.rest;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oguerreiro.resilient.repository.InputDataRepository;
import com.oguerreiro.resilient.service.InputDataService;
import com.oguerreiro.resilient.service.OrganizationService;
import com.oguerreiro.resilient.service.criteria.InputDataCriteria;
import com.oguerreiro.resilient.service.dto.InventoryDataDTO;
import com.oguerreiro.resilient.service.mapper.InventoryDataMapper;
import com.oguerreiro.resilient.service.query.InputDataQueryService;

/**
 * REST controller for managing Inventory requests. Inventory is a POJO for frontend InpuData view.
 */
@RestController
@RequestMapping("/api/inventory")
public class InventoryResource {

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

  private static final String ENTITY_NAME = "inventory";

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

  private final InputDataService inputDataService;

  public InventoryResource(InputDataRepository inputDataRepository, InputDataQueryService inputDataQueryService,
      InventoryDataMapper inventoryDataMapper, InputDataService inputDataService,
      OrganizationService organizationService) {
    this.inputDataService = inputDataService;

  }

  /**
   * {@code GET  /inventory} : get all the outputData.
   *
   * @param  criteria the criteria which the requested entities should match.
   * @return          the {@link ResponseEntity} with status {@code 200 (OK)} and the list of inventory in body.
   */
  @PreAuthorize("@resilientSecurity.canAccess('READ', 'com.oguerreiro.resilient.domain.InputData')")  // Inventory, delegates permission to InputData
  @GetMapping("")
  public ResponseEntity<List<InventoryDataDTO>> searchInventory(InputDataCriteria criteria) {
    log.debug("REST request to searchInventory by criteria: {}", criteria);

    Long periodId = criteria.getPeriodId() == null ? null : criteria.getPeriodId().getEquals();
    Long ownerId = criteria.getOwnerId() == null ? null : criteria.getOwnerId().getEquals();
    Long scopeId = criteria.getScopeId() == null ? null : criteria.getScopeId().getEquals();
    Long categoryId = criteria.getCategoryId() == null ? null : criteria.getCategoryId().getEquals();

    List<InventoryDataDTO> entityList = inputDataService.searchInventory(periodId, ownerId, scopeId, categoryId);
    return ResponseEntity.ok().body(entityList);
  }

  @PreAuthorize("@resilientSecurity.canAccess('READ', 'com.oguerreiro.resilient.domain.InputData')") // Inventory, delegates permission to InputData
  @GetMapping("/export")
  public ResponseEntity<byte[]> export(InputDataCriteria criteria) {
    log.debug("REST request to export : {}");

    Long periodId = criteria.getPeriodId() == null ? null : criteria.getPeriodId().getEquals();
    Long ownerId = criteria.getOwnerId() == null ? null : criteria.getOwnerId().getEquals();
    Long scopeId = criteria.getScopeId() == null ? null : criteria.getScopeId().getEquals();
    Long categoryId = criteria.getCategoryId() == null ? null : criteria.getCategoryId().getEquals();

    byte[] fileContent = this.inputDataService.exportInventory(periodId, ownerId, scopeId, categoryId);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    headers.setContentDisposition(ContentDisposition.attachment().filename("report.xlsx").build());

    return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
  }
}
