package de.thpeetz.kontor;

import java.util.HashMap;
import java.util.LinkedList;

import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.thpeetz.kontor.infrastructure.AppHibernateSessionFactory;
import de.thpeetz.kontor.models.comics.Comic;
import de.thpeetz.kontor.models.media.MediaFile;
import io.javalin.Javalin;
import io.javalin.config.Key;
//import jakarta.persistence.EntityManager;

public class Main {
    private static Logger logger = LoggerFactory.getLogger(Main.class);
    private static short port = 8600;
    // private static Key<EntityManagerFactory> emf = new
    // Key<EntityManagerFactory>("entityManagerFactory");
    private static Key<SessionFactory> sf = new Key<>("SessionFactory");

    public static void main(String[] args) {
        Javalin app = Javalin.create(config -> {
            config.requestLogger.http((ctx, ms) -> {
                var path = ctx.path();
                if (!path.equals("/health")) {
                    logger.info(ctx.path());
                }
            });
            config.appData(sf, AppHibernateSessionFactory.getSessionFactory());
        });
        app.get("/", ctx -> ctx.json("Ok"));
        app.get("/health", ctx -> {
            HashMap<String, String> status = new HashMap<>();
            status.put("status", "ok");
            ctx.json(status);
        });
        app.get("/api/v1/comics", ctx -> {
            var result = new LinkedList<Comic>();
            ctx.appData(sf).inStatelessTransaction(session -> {
                session.createSelectionQuery("from Comic", Comic.class).getResultList().forEach(comic -> {
                    result.add(comic);
                });
            });
            // var em = ctx.appData(emf).createEntityManager();
            // logger.info(em.toString());
            // EntityManager entityManager = ctx.attribute("entityManager");
            // entityManager.createQuery("select * from Comic",
            // Comic.class).getResultList().forEach(comic -> {
            // result.add(comic);
            // });
            ctx.json(result);
        });
        app.get("/api/v1/media/files", ctx -> {
            var result = new LinkedList<>();
            // ctx.appData(sf).inStatelessTransaction(session -> {
            // var files = session.createSelectionQuery("from MediaFile",
            // MediaFile.class).getResultList();
            // result.addAll(files);
            // });
            var list = ctx.appData(sf).openStatelessSession().createSelectionQuery("from MediaFile", MediaFile.class)
                    .getResultList();
            result.addAll(list);
            ctx.json(result);
        });
        // app.before(ctx -> {
        // ctx.attribute("configuration", AppHibernateConfig.configuration());
        // ctx.attribute("sessionFactory",
        // AppHibernateSessionFactory.getSessionFactory());
        // });
        app.after(ctx -> {
            // SessionFactory sessionFactory = ctx.attribute("sessionFactory");
            // if (sessionFactory != null) {
            // logger.info("close SessionFactory");
            // sessionFactory.close();
            // }
            // EntityManager entityManager = ctx.attribute("entityManager");
            // if (entityManager != null) {
            // logger.info("close EntityManager");
            // entityManager.close();
            // }
        });
        app.start(port);

        logger.info("API's alive for real :-)");
    }
}
