
package eu.dnetlib.dhp.oa.graph.dump.filterentities;

import static eu.dnetlib.dhp.common.SparkSessionSupport.runWithSparkSession;

import java.io.Serializable;
import java.util.Optional;

import org.apache.commons.io.IOUtils;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FilterFunction;
import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.*;
import org.apache.spark.sql.functions.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.dnetlib.dhp.application.ArgumentApplicationParser;
import eu.dnetlib.dhp.oa.graph.dump.skgif.EmitFromEntities;
import eu.dnetlib.dhp.oa.graph.dump.skgif.Utils;
import eu.dnetlib.dhp.schema.common.ModelSupport;
import eu.dnetlib.dhp.schema.oaf.Result;
import scala.Tuple2;

/**
 * @author miriam.baglioni
 * @Date 20/03/24
 */
public class SelectEOSCEntities implements Serializable {
	private static final Logger log = LoggerFactory.getLogger(SelectEOSCEntities.class);
	private static final String B2FIND_IDENTIFIER = "10|re3data_____::730f562f9efe8a3b3742d2da510d4335";

	public static void main(String[] args) throws Exception {
		String jsonConfiguration = IOUtils
			.toString(
				FilterEntities.class
					.getResourceAsStream(
						"/eu/dnetlib/dhp/oa/graph/dump/skgif/eosc_entities_parameters.json"));

		final ArgumentApplicationParser parser = new ArgumentApplicationParser(jsonConfiguration);
		parser.parseArgument(args);

		Boolean isSparkSessionManaged = Optional
			.ofNullable(parser.get("isSparkSessionManaged"))
			.map(Boolean::valueOf)
			.orElse(Boolean.TRUE);

		log.info("isSparkSessionManaged: {}", isSparkSessionManaged);

		final String inputPath = parser.get("sourcePath");
		log.info("inputPath: {}", inputPath);

		final String filterPath = parser.get("filterPath");
		log.info("filterPath: {}", filterPath);

		SparkConf conf = new SparkConf();

		runWithSparkSession(
			conf,
			isSparkSessionManaged,
			spark -> {

				selectEntities(spark, inputPath, filterPath);

			});
	}

	private static <R extends Result> void selectEntities(SparkSession spark, String inputPath, String filterPath) {
		ModelSupport.entityTypes.keySet().forEach(e -> {
			if (ModelSupport.isResult(e)) {
//				Utils
//					.readPath(spark, inputPath + e.name(), ModelSupport.entityTypes.get(e))
//					.filter(
//						(FilterFunction<R>) r -> !r.getDataInfo().getDeletedbyinference()
//							&& !r.getDataInfo().getInvisible()
//							&& (r.getContext().stream().anyMatch(c -> c.getId().equals("eosc")) ||
//								r
//									.getCollectedfrom()
//									.stream()
//									.anyMatch(cf -> cf.getValue().equalsIgnoreCase("B2FIND"))))
//					.map((MapFunction<R, String>) r -> r.getId(), Encoders.STRING())

				spark
					.read()
					.schema(Encoders.bean(Result.class).schema())
					.json(inputPath + e.name())
					.where("datainfo.deletedbyinference != true and datainfo.invisible != true")
					.select("id", "context", "collectedfrom")
					.where("array_contains(context.id,'eosc') or array_contains(collectedfrom.value,'B2FIND')")
					.drop("context", "collectedfrom")
					.distinct()
					.write()
					.mode(SaveMode.Overwrite)
					.option("compression", "gzip")
					.parquet(filterPath + e.name() + "_ids");

//
			}

		});
	}

}
