package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBMaintenanceDetailsService;
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.TBMaintenanceDetailsDTO;
import vn.azteam.tpf.service.dto.TBMaintenanceDetailsCriteria;
import vn.azteam.tpf.service.TBMaintenanceDetailsQueryService;
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 TBMaintenanceDetails.
 */
@RestController
@RequestMapping("/api")
public class TBMaintenanceDetailsResource {

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

    private static final String ENTITY_NAME = "tBMaintenanceDetails";

    private final TBMaintenanceDetailsService tBMaintenanceDetailsService;

    private final TBMaintenanceDetailsQueryService tBMaintenanceDetailsQueryService;

    public TBMaintenanceDetailsResource(TBMaintenanceDetailsService tBMaintenanceDetailsService, TBMaintenanceDetailsQueryService tBMaintenanceDetailsQueryService) {
        this.tBMaintenanceDetailsService = tBMaintenanceDetailsService;
        this.tBMaintenanceDetailsQueryService = tBMaintenanceDetailsQueryService;
    }

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

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

    /**
     * GET  /tb-maintenance-details : get all the tBMaintenanceDetails.
     *
     * @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 tBMaintenanceDetails in body
     */
    @GetMapping("/tb-maintenance-details")
    @Timed
    public ResponseEntity<List<TBMaintenanceDetailsDTO>> getAllTBMaintenanceDetails(TBMaintenanceDetailsCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBMaintenanceDetails by criteria: {}", criteria);
        Page<TBMaintenanceDetailsDTO> page = tBMaintenanceDetailsQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-maintenance-details");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-maintenance-details/count : count all the tBMaintenanceDetails.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-maintenance-details/count")
    @Timed
    public ResponseEntity<Long> countTBMaintenanceDetails(TBMaintenanceDetailsCriteria criteria) {
        log.debug("REST request to count TBMaintenanceDetails by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBMaintenanceDetailsQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-maintenance-details/:id : get the "id" tBMaintenanceDetails.
     *
     * @param id the id of the tBMaintenanceDetailsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBMaintenanceDetailsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-maintenance-details/{id}")
    @Timed
    public ResponseEntity<TBMaintenanceDetailsDTO> getTBMaintenanceDetails(@PathVariable Long id) {
        log.debug("REST request to get TBMaintenanceDetails : {}", id);
        Optional<TBMaintenanceDetailsDTO> tBMaintenanceDetailsDTO = tBMaintenanceDetailsService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBMaintenanceDetailsDTO);
    }

    /**
     * DELETE  /tb-maintenance-details/:id : delete the "id" tBMaintenanceDetails.
     *
     * @param id the id of the tBMaintenanceDetailsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-maintenance-details/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBMaintenanceDetails(@PathVariable Long id) {
        log.debug("REST request to delete TBMaintenanceDetails : {}", id);
        tBMaintenanceDetailsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
