package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBBlockDetailsReportService;
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.TBBlockDetailsReportDTO;
import vn.azteam.tpf.service.dto.TBBlockDetailsReportCriteria;
import vn.azteam.tpf.service.TBBlockDetailsReportQueryService;
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 TBBlockDetailsReport.
 */
@RestController
@RequestMapping("/api")
public class TBBlockDetailsReportResource {

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

    private static final String ENTITY_NAME = "tBBlockDetailsReport";

    private final TBBlockDetailsReportService tBBlockDetailsReportService;

    private final TBBlockDetailsReportQueryService tBBlockDetailsReportQueryService;

    public TBBlockDetailsReportResource(TBBlockDetailsReportService tBBlockDetailsReportService, TBBlockDetailsReportQueryService tBBlockDetailsReportQueryService) {
        this.tBBlockDetailsReportService = tBBlockDetailsReportService;
        this.tBBlockDetailsReportQueryService = tBBlockDetailsReportQueryService;
    }

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

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

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

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

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

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

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

}
