package es.uv.saic.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import es.uv.saic.domain.Proces;
import es.uv.saic.dto.ProcesDTO;
import es.uv.saic.service.ProcesService;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;



@RestController
public class ProcesController {
    @Autowired
    private ProcesService procesService;

    @PostMapping("/proces/{id}")
    public ResponseEntity<?> findById(@PathVariable Integer id) {
        try {
            Proces proces = procesService.findByID(id);
            ProcesDTO procesDTO = new ProcesDTO(proces);

            return (procesDTO != null) ? ResponseEntity.ok(procesDTO) : ResponseEntity.badRequest().body("El proces con id " + id + " esta vacio");
        } catch (Exception e) {
             e.printStackTrace();
            return ResponseEntity.badRequest().body("Error al obtener el proces por el id:" +  e.getMessage());
        }
    }
    
    @GetMapping("/proces/getAll")
    public ResponseEntity<?> getAll() {
        try {
            List<Proces> proces = procesService.getAll();
            List<ProcesDTO> procesDTOList = new ArrayList<>();
            
            for(Proces pro: proces) {
                procesDTOList.add(new ProcesDTO(pro));
            }

            return ResponseEntity.ok(procesDTOList);
        } catch(Exception e) {
            return ResponseEntity.badRequest().body("Error al obtener los centros");
        }
    }
    
}
