package fr.doap.qodo;

import io.quarkus.security.identity.*;
import jakarta.inject.*;
import jakarta.persistence.*;
import jakarta.transaction.*;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import org.eclipse.microprofile.jwt.*;

import java.util.*;

@Path("/hello")
public class GreetingResource {
	@Inject
	SecurityIdentity mIdentity;

	@Inject
	JsonWebToken mPrincipal;

	@Inject
	EntityManager	mEntityManager;
	
//	@RolesAllowed("user")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
	@Transactional
    public List<MyEntity> hello() {
		System.out.println("Identity: "+mIdentity);
		System.out.println("Principal: "+mIdentity.getPrincipal());
		
		System.out.println("JWT Identity: "+mPrincipal);
		System.out.println("JWT Principal: "+mPrincipal.getName());

		MyEntity lM = new MyEntity();
		lM.field = "Test from "+mPrincipal.getName()+" @ "+new Date();
		lM.field = "Test from "+mIdentity.getPrincipal()+" @ "+new Date();
		
//		mEntityManager.getTransaction().begin();
		mEntityManager.persist(lM);
//		mEntityManager.getTransaction().commit();
		
		List<MyEntity> lMyEntities = mEntityManager.createQuery("SELECT me FROM MyEntity me", MyEntity.class).getResultList();
		
		return lMyEntities;
//        return "Hello from Quarkus REST";
    }
}
