package fr.doap.qodo;

import jakarta.inject.*;
import jakarta.persistence.*;
import jakarta.transaction.*;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import org.slf4j.*;

import java.util.*;

@Path("/v1/qodos")
public class QodoService {
	
	private static final Logger log = LoggerFactory.getLogger(QodoService.class);
	@Inject
	EntityManager mEntityManager;
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<Qodo> listAll(@Context UriInfo aUriInfo) {
		return Qodo.listAll();

		/*
		MultivaluedMap<String, String> lQueryParams = aUriInfo.getQueryParameters();
		
		Sort lSort = null;
		if (lQueryParams.containsKey("sort")) {
		
		}
		
		
		PanacheQuery<Qodo> lQodoAll;
		if (lSort == null) lQodoAll = Qodo.findAll();
		else lQodoAll = Qodo.findAll(lSort);
		 */
		
//		return mEntityManager.createQuery("SELECT qodos FROM Qodo qodos", Qodo.class).getResultList();
	}
	
	@GET
	@Path("{id : \\d+}")
	@Produces(MediaType.APPLICATION_JSON)
	public Qodo findById(@PathParam("id") long aID) {
		return Qodo.findById(aID);
		
//		List<Qodo> lRes = mEntityManager.createQuery("SELECT qodos FROM Qodo qodos WHERE ", Qodo.class).getResultList();
	}
	
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@Transactional
	public Response createNew(Qodo aNew) {
		aNew.persist();
		return Response.status(Response.Status.CREATED).entity(aNew).build();
	}
	
	@PUT
	@Path("{id : \\d+}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@Transactional
	public Response update(@PathParam("id") long aID, Qodo aUpdated) {
		aUpdated.id = aID;
		Qodo.getEntityManager().merge(aUpdated);
		return Response.status(Response.Status.OK).entity(aUpdated).build();
	}
	
	@DELETE
	@Path("{id : \\d+}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@Transactional
	public Response delete(@PathParam("id") long aID) {
		Qodo.deleteById(aID);
		return Response.status(Response.Status.OK).build();
	}
}