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("strafe")
public class StrafeResource {

	private Logger LOGGER = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext(name = "aufgaben")
	private EntityManager entityManager;
	@Resource
	private UserTransaction transaction;
	
	@Path("{strafeId}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response get(@PathParam("strafeId") UUID strafeId, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			transaction.begin();
			StrafeEntity entity = new StrafeDAO(entityManager).findById(strafeId);
			if (entity != null) {
				Strafe strafe = entity.toStrafe();
				transaction.commit();
				return Response.ok(strafe).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(Strafe strafe, @Context UriInfo uriInfo, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			if (strafe.getKurzText() == null || strafe.getText() == null || strafe.getLevel() == null || strafe.getGruppeId() == null) {
				return Response.status(Status.BAD_REQUEST).build();
			}
			transaction.begin();
			AufgabenGruppeEntity gruppeEntity = new AufgabenGruppeDAO(entityManager).findById(strafe.getGruppeId());
			if (gruppeEntity == null || gruppeEntity.getAufgaben().size() > 50) {
				transaction.rollback();
				return Response.status(Status.BAD_REQUEST).build();
			}
			StrafeEntity entity = StrafeEntity.create(strafe, gruppeEntity);
			new StrafeDAO(entityManager).persist(entity);
			transaction.commit();
			return Response.created(uriInfo.getAbsolutePathBuilder().path(entity.getStrafeId().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(Strafe strafe, @Context HttpServletRequest req) throws IllegalStateException, SecurityException, SystemException {
		try {
			RequestChecker.checkRequest(req);
			transaction.begin();
			StrafeDAO dao = new StrafeDAO(entityManager);
			StrafeEntity entity = dao.findById(strafe.getStrafeId());
			dao.remove(entity);
			transaction.commit();
			return Response.accepted().build();
		} catch (Exception exception) {
			LOGGER.error(exception.getMessage(), exception);
			transaction.rollback();
			return Response.serverError().build();
		}

	}
}
