package com.pmu.betengine.controller; import com.pmu.betengine.model.TPE; import com.pmu.betengine.model.dto.TPEDTO; import com.pmu.betengine.model.dto.TPERequestDTO; import com.pmu.betengine.model.statut.StatutTPE; import com.pmu.betengine.service.TPEService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; @RestController @RequestMapping("/api/v1/tpes") @CrossOrigin(origins = "*") @Tag(name = "Gestion des TPE", description = "Endpoints relatifs à l'objet TPE") public class TPEController { private final TPEService tpeService; public TPEController(TPEService tpeService) { this.tpeService = tpeService; } @GetMapping public ResponseEntity> getAllTPEs() { return ResponseEntity.ok(tpeService.getAllTPEs()); } @GetMapping("/{id}") public ResponseEntity getTPEById(@PathVariable Long id) { return ResponseEntity.ok(tpeService.getTPEById(id)); } @PostMapping public ResponseEntity createTPE(@RequestBody TPE tpe) { return new ResponseEntity<>(tpeService.createTPE(tpe), HttpStatus.CREATED); } @PutMapping("/{id}") public ResponseEntity updateTPE(@PathVariable Long id, @RequestBody TPE tpe) { return ResponseEntity.ok(tpeService.updateTPE(id, tpe)); } @DeleteMapping("/{id}") public ResponseEntity deleteTPE(@PathVariable Long id) { tpeService.deleteTPE(id); return ResponseEntity.noContent().build(); } @PatchMapping("/{id}/statut") public ResponseEntity updateStatut(@PathVariable Long id, @RequestBody Map request) { String statutStr = request.get("statut"); StatutTPE statut = StatutTPE.valueOf(statutStr.toUpperCase()); return ResponseEntity.ok(tpeService.updateStatut(id, statut)); } // @PatchMapping("/{id}/assigner") // public ResponseEntity assignerTPE(@PathVariable Long id, @RequestParam Long agentId) { // return ResponseEntity.ok(tpeService.assignerTPE(id, agentId)); // } @PatchMapping("/assigner") @Operation(summary = "Assigner Un TPE", description = "Assigner un TPE à un Agent") public ResponseEntity assignerTPE(@RequestBody Map request) { Long tpeId = request.get("tpeId"); Long agentId = request.get("agentId"); return ResponseEntity.ok(tpeService.assignerTPE(tpeId, agentId)); } @PatchMapping("/liberer/{id}") public ResponseEntity libererTPE(@PathVariable Long id) { return ResponseEntity.ok(tpeService.libererTPE(id)); } @GetMapping("/statut/{statut}") public ResponseEntity> getTPEsByStatut(@PathVariable StatutTPE statut) { return ResponseEntity.ok(tpeService.getTPEsByStatut(statut)); } @GetMapping("/disponibles") public ResponseEntity> getTPEsDisponibles() { return ResponseEntity.ok(tpeService.getTPEsDisponibles()); } @GetMapping("/search") public ResponseEntity> searchTPEs(@RequestParam String q) { return ResponseEntity.ok(tpeService.searchTPEs(q)); } @GetMapping("/stats/count-by-statut") public ResponseEntity> getCountByStatut() { return ResponseEntity.ok(Map.of( StatutTPE.VALIDE, tpeService.countTPEsByStatut(StatutTPE.VALIDE), StatutTPE.INVALIDE, tpeService.countTPEsByStatut(StatutTPE.INVALIDE), StatutTPE.EN_PANNE, tpeService.countTPEsByStatut(StatutTPE.EN_PANNE), StatutTPE.BLOQUE, tpeService.countTPEsByStatut(StatutTPE.BLOQUE), StatutTPE.DISPONIBLE, tpeService.countTPEsByStatut(StatutTPE.DISPONIBLE), StatutTPE.AFFECTE, tpeService.countTPEsByStatut(StatutTPE.AFFECTE), StatutTPE.EN_MAINTENANCE, tpeService.countTPEsByStatut(StatutTPE.EN_MAINTENANCE), StatutTPE.HORS_SERVICE, tpeService.countTPEsByStatut(StatutTPE.HORS_SERVICE), StatutTPE.VOLE, tpeService.countTPEsByStatut(StatutTPE.VOLE) )); } @GetMapping("/stats/assignes") public ResponseEntity getCountAssignes() { return ResponseEntity.ok(tpeService.countTPEsAssignes()); } }