package de.oaa.xxx.registration;

import java.util.UUID;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;

@ApplicationScoped
@Path("activation")
public class ActivationResource {

	@Inject private RegistrationDAO registrationDao;

	@GET
	@Path("{uuid}")
	public Response activate(@PathParam("uuid") String registrationId, @Context UriInfo info) {
		RegistrationEntity registration = registrationDao.findById(UUID.fromString(registrationId));
		if (registration != null && !Boolean.TRUE.equals(registration.getActivated())) {
			Response response = ClientBuilder.newClient().target("http://localhost:8080/user-service/user").request().post(Entity.entity(registration.toRegistration(), MediaType.APPLICATION_JSON));
			if (200 == response.getStatus()) {				
				registration.setActivated(Boolean.TRUE);
				registrationDao.update(registration);
				return Response.seeOther(info.getBaseUriBuilder().path("activation.html").build()).build();
			} else {
				return Response.serverError().build();
			}
		} else {
			return Response.noContent().build();
		}
	}
}
