package de.oaa.xxx.aufgaben;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.oaa.xxx.framework.request.RequestChecker;
import jakarta.annotation.Resource;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.SystemException;
import jakarta.transaction.UserTransaction;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;

@ApplicationScoped
@Path("sperre")
public class SprerreResource {

	private Logger LOGGER = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext(name = "aufgaben")
	private EntityManager entityManager;
	@Resource
	private UserTransaction transaction;
		
	
	@Path("{sperreId}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response get(@PathParam("sperreId") UUID sperreId, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			transaction.begin();
			SperreEntity entity = new SperreDAO(entityManager).findById(sperreId);
			if (entity != null) {
				Sperre sperre = entity.toSperre();
				transaction.commit();
				return Response.ok(sperre).build();
			} else {
				transaction.rollback();
				return Response.noContent().build();
			}
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}
	}
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response create(Sperre sperre, @Context UriInfo uriInfo, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			if (sperre.getKurzText() == null || sperre.getText() == null || sperre.getMinutenVon() == null || sperre.getGruppeId() == null || sperre.getSperreFuer() == null || sperre.getSperreFuer().isEmpty()) {
				return Response.status(Status.BAD_REQUEST).build();
			}
			transaction.begin();
			AufgabenGruppeEntity gruppeEntity = new AufgabenGruppeDAO(entityManager).findById(sperre.getGruppeId());
			if (gruppeEntity == null || gruppeEntity.getAufgaben().size() > 50) {
				transaction.rollback();
				return Response.status(Status.BAD_REQUEST).build();
			}
			SperreEntity entity = SperreEntity.create(sperre, gruppeEntity);
			new SperreDAO(entityManager).persist(entity);
			transaction.commit();
			return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getSperreId().toString()).build()).build();
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}
	}
	
	@DELETE
	@Consumes(MediaType.APPLICATION_JSON)
	public Response delete(Sperre sperre, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			transaction.begin();
			SperreDAO dao = new SperreDAO(entityManager);
			SperreEntity entity = dao.findById(sperre.getSperreId());
			dao.remove(entity);
			transaction.commit();
			return Response.accepted().build();
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}
	}
}
