package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBProductBlockDetailsReportService;
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.TBProductBlockDetailsReportDTO;
import vn.azteam.tpf.service.dto.TBProductBlockDetailsReportCriteria;
import vn.azteam.tpf.service.TBProductBlockDetailsReportQueryService;
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 TBProductBlockDetailsReport.
 */
@RestController
@RequestMapping("/api")
public class TBProductBlockDetailsReportResource {

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

    private static final String ENTITY_NAME = "tBProductBlockDetailsReport";

    private final TBProductBlockDetailsReportService tBProductBlockDetailsReportService;

    private final TBProductBlockDetailsReportQueryService tBProductBlockDetailsReportQueryService;

    public TBProductBlockDetailsReportResource(TBProductBlockDetailsReportService tBProductBlockDetailsReportService, TBProductBlockDetailsReportQueryService tBProductBlockDetailsReportQueryService) {
        this.tBProductBlockDetailsReportService = tBProductBlockDetailsReportService;
        this.tBProductBlockDetailsReportQueryService = tBProductBlockDetailsReportQueryService;
    }

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

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

    /**
     * GET  /tb-product-block-details-reports : get all the tBProductBlockDetailsReports.
     *
     * @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 tBProductBlockDetailsReports in body
     */
    @GetMapping("/tb-product-block-details-reports")
    @Timed
    public ResponseEntity<List<TBProductBlockDetailsReportDTO>> getAllTBProductBlockDetailsReports(TBProductBlockDetailsReportCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBProductBlockDetailsReports by criteria: {}", criteria);
        Page<TBProductBlockDetailsReportDTO> page = tBProductBlockDetailsReportQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-product-block-details-reports");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-product-block-details-reports/count : count all the tBProductBlockDetailsReports.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-product-block-details-reports/count")
    @Timed
    public ResponseEntity<Long> countTBProductBlockDetailsReports(TBProductBlockDetailsReportCriteria criteria) {
        log.debug("REST request to count TBProductBlockDetailsReports by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBProductBlockDetailsReportQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-product-block-details-reports/:id : get the "id" tBProductBlockDetailsReport.
     *
     * @param id the id of the tBProductBlockDetailsReportDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBProductBlockDetailsReportDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-product-block-details-reports/{id}")
    @Timed
    public ResponseEntity<TBProductBlockDetailsReportDTO> getTBProductBlockDetailsReport(@PathVariable Long id) {
        log.debug("REST request to get TBProductBlockDetailsReport : {}", id);
        Optional<TBProductBlockDetailsReportDTO> tBProductBlockDetailsReportDTO = tBProductBlockDetailsReportService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBProductBlockDetailsReportDTO);
    }

    /**
     * DELETE  /tb-product-block-details-reports/:id : delete the "id" tBProductBlockDetailsReport.
     *
     * @param id the id of the tBProductBlockDetailsReportDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-product-block-details-reports/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBProductBlockDetailsReport(@PathVariable Long id) {
        log.debug("REST request to delete TBProductBlockDetailsReport : {}", id);
        tBProductBlockDetailsReportService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/tb-product-block-details-reports?query=:query : search for the tBProductBlockDetailsReport corresponding
     * to the query.
     *
     * @param query the query of the tBProductBlockDetailsReport search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/tb-product-block-details-reports")
    @Timed
    public ResponseEntity<List<TBProductBlockDetailsReportDTO>> searchTBProductBlockDetailsReports(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of TBProductBlockDetailsReports for query {}", query);
        Page<TBProductBlockDetailsReportDTO> page = tBProductBlockDetailsReportService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-product-block-details-reports");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
