package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBSuppliesPriceHistoryService;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;
import vn.azteam.tpf.web.rest.util.HeaderUtil;
import vn.azteam.tpf.web.rest.util.PaginationUtil;
import vn.azteam.tpf.service.dto.TBSuppliesPriceHistoryDTO;
import vn.azteam.tpf.service.dto.TBSuppliesPriceHistoryCriteria;
import vn.azteam.tpf.service.TBSuppliesPriceHistoryQueryService;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing TBSuppliesPriceHistory.
 */
@RestController
@RequestMapping("/api")
public class TBSuppliesPriceHistoryResource {

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

    private static final String ENTITY_NAME = "tBSuppliesPriceHistory";

    private final TBSuppliesPriceHistoryService tBSuppliesPriceHistoryService;

    private final TBSuppliesPriceHistoryQueryService tBSuppliesPriceHistoryQueryService;

    public TBSuppliesPriceHistoryResource(TBSuppliesPriceHistoryService tBSuppliesPriceHistoryService, TBSuppliesPriceHistoryQueryService tBSuppliesPriceHistoryQueryService) {
        this.tBSuppliesPriceHistoryService = tBSuppliesPriceHistoryService;
        this.tBSuppliesPriceHistoryQueryService = tBSuppliesPriceHistoryQueryService;
    }

    /**
     * POST  /tb-supplies-price-histories : Create a new tBSuppliesPriceHistory.
     *
     * @param tBSuppliesPriceHistoryDTO the tBSuppliesPriceHistoryDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new tBSuppliesPriceHistoryDTO, or with status 400 (Bad Request) if the tBSuppliesPriceHistory has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/tb-supplies-price-histories")
    @Timed
    public ResponseEntity<TBSuppliesPriceHistoryDTO> createTBSuppliesPriceHistory(@RequestBody TBSuppliesPriceHistoryDTO tBSuppliesPriceHistoryDTO) throws URISyntaxException {
        log.debug("REST request to save TBSuppliesPriceHistory : {}", tBSuppliesPriceHistoryDTO);
        if (tBSuppliesPriceHistoryDTO.getId() != null) {
            throw new BadRequestAlertException("A new tBSuppliesPriceHistory cannot already have an ID", ENTITY_NAME, "idexists");
        }
        TBSuppliesPriceHistoryDTO result = tBSuppliesPriceHistoryService.save(tBSuppliesPriceHistoryDTO);
        return ResponseEntity.created(new URI("/api/tb-supplies-price-histories/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /tb-supplies-price-histories : Updates an existing tBSuppliesPriceHistory.
     *
     * @param tBSuppliesPriceHistoryDTO the tBSuppliesPriceHistoryDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated tBSuppliesPriceHistoryDTO,
     * or with status 400 (Bad Request) if the tBSuppliesPriceHistoryDTO is not valid,
     * or with status 500 (Internal Server Error) if the tBSuppliesPriceHistoryDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/tb-supplies-price-histories")
    @Timed
    public ResponseEntity<TBSuppliesPriceHistoryDTO> updateTBSuppliesPriceHistory(@RequestBody TBSuppliesPriceHistoryDTO tBSuppliesPriceHistoryDTO) throws URISyntaxException {
        log.debug("REST request to update TBSuppliesPriceHistory : {}", tBSuppliesPriceHistoryDTO);
        if (tBSuppliesPriceHistoryDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        TBSuppliesPriceHistoryDTO result = tBSuppliesPriceHistoryService.save(tBSuppliesPriceHistoryDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tBSuppliesPriceHistoryDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /tb-supplies-price-histories : get all the tBSuppliesPriceHistories.
     *
     * @param pageable the pagination information
     * @param criteria the criterias which the requested entities should match
     * @return the ResponseEntity with status 200 (OK) and the list of tBSuppliesPriceHistories in body
     */
    @GetMapping("/tb-supplies-price-histories")
    @Timed
    public ResponseEntity<List<TBSuppliesPriceHistoryDTO>> getAllTBSuppliesPriceHistories(TBSuppliesPriceHistoryCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBSuppliesPriceHistories by criteria: {}", criteria);
        Page<TBSuppliesPriceHistoryDTO> page = tBSuppliesPriceHistoryQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-supplies-price-histories");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-supplies-price-histories/count : count all the tBSuppliesPriceHistories.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-supplies-price-histories/count")
    @Timed
    public ResponseEntity<Long> countTBSuppliesPriceHistories(TBSuppliesPriceHistoryCriteria criteria) {
        log.debug("REST request to count TBSuppliesPriceHistories by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBSuppliesPriceHistoryQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-supplies-price-histories/:id : get the "id" tBSuppliesPriceHistory.
     *
     * @param id the id of the tBSuppliesPriceHistoryDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBSuppliesPriceHistoryDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-supplies-price-histories/{id}")
    @Timed
    public ResponseEntity<TBSuppliesPriceHistoryDTO> getTBSuppliesPriceHistory(@PathVariable Long id) {
        log.debug("REST request to get TBSuppliesPriceHistory : {}", id);
        Optional<TBSuppliesPriceHistoryDTO> tBSuppliesPriceHistoryDTO = tBSuppliesPriceHistoryService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBSuppliesPriceHistoryDTO);
    }

    /**
     * DELETE  /tb-supplies-price-histories/:id : delete the "id" tBSuppliesPriceHistory.
     *
     * @param id the id of the tBSuppliesPriceHistoryDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-supplies-price-histories/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBSuppliesPriceHistory(@PathVariable Long id) {
        log.debug("REST request to delete TBSuppliesPriceHistory : {}", id);
        tBSuppliesPriceHistoryService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/tb-supplies-price-histories?query=:query : search for the tBSuppliesPriceHistory corresponding
     * to the query.
     *
     * @param query the query of the tBSuppliesPriceHistory search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/tb-supplies-price-histories")
    @Timed
    public ResponseEntity<List<TBSuppliesPriceHistoryDTO>> searchTBSuppliesPriceHistories(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of TBSuppliesPriceHistories for query {}", query);
        Page<TBSuppliesPriceHistoryDTO> page = tBSuppliesPriceHistoryService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-supplies-price-histories");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
