package ankurm.jev;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springaicommunity.typesafe.TypeSafeClient;
import org.springaicommunity.typesafe.autoconfigure.TypeSafeAutoConfiguration;
import org.springaicommunity.typesafe.question.Choice;
import org.springaicommunity.typesafe.question.Noul;
import org.springaicommunity.typesafe.question.Question;
import org.springaicommunity.typesafe.response.Answer;
import org.springaicommunity.typesafe.response.ChoiceAnswer;
import org.springaicommunity.typesafe.response.NoulAnswer;
import org.springaicommunity.typesafe.response.SystemOneResponse;
import org.springaicommunity.typesafe.response.Usage;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

/**
 * Jev starter 0.4.0, offline. Part 1 shows when the TypeSafeClient bean exists. Part 2 builds the questions you would
 * send. Part 3 routes on hand-built responses. Nothing here calls the TypeSafe service.
 */
public class JevRoutingDemo {

	/** Questions a router would ask about a support ticket. */
	static Map<String, Question> routerQuestions() {
		Map<String, Question> q = new LinkedHashMap<>();
		q.put("difficulty", Choice.of("How hard is it to answer this ticket correctly?", "simple", "complex"));
		q.put("needs_account_data", Noul.of("Does answering require looking at the customer's account data?"));
		return q;
	}

	enum Route { SMALL_MODEL, LARGE_MODEL }

	/** Fast path only when Jev is confident the ticket is simple; anything unclear goes to the large model. */
	static Route route(SystemOneResponse r, double minConfidence) {
		ChoiceAnswer difficulty = r.choice("difficulty");
		if ("simple".equals(difficulty.value()) && difficulty.confidence() >= minConfidence) {
			return Route.SMALL_MODEL;
		}
		return Route.LARGE_MODEL;
	}

	static SystemOneResponse fake(String label, double confidence, double needsData) {
		Map<String, Answer> answers = new LinkedHashMap<>();
		Map<String, Double> p = Map.of(label, confidence, "simple".equals(label) ? "complex" : "simple", 1 - confidence);
		answers.put("difficulty", new ChoiceAnswer(label, p, confidence));
		answers.put("needs_account_data", new NoulAnswer(needsData));
		return new SystemOneResponse("jev-demo", answers, new Usage(40, 4));
	}

	public static void main(String[] args) {
		System.out.println("== 1. when does a TypeSafeClient bean exist?");
		ApplicationContextRunner runner = new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(TypeSafeAutoConfiguration.class));
		runner.run(ctx -> System.out.println("no api key  -> TypeSafeClient beans: " + ctx.getBeansOfType(TypeSafeClient.class).size()));
		runner.withPropertyValues("spring.ai.typesafe.api-key=demo-key-never-sent").run(ctx -> {
			System.out.println("with api key -> TypeSafeClient beans: " + ctx.getBeansOfType(TypeSafeClient.class).size());
			if (ctx.getBeansOfType(TypeSafeClient.class).size() == 1) {
				TypeSafeClient c = ctx.getBean(TypeSafeClient.class);
				System.out.println("default model = " + c.defaultModel() + ", timeout = " + c.timeout());
			}
		});

		System.out.println("== 2. the questions that would be sent");
		routerQuestions().forEach((k, v) -> System.out.println(k + " -> " + v.typeName() + ": " + v.instructions()));

		System.out.println("== 3. routing on hand-built responses (not from the service)");
		double threshold = 0.8;
		Object[][] cases = { { "simple", 0.95 }, { "simple", 0.60 }, { "complex", 0.90 } };
		for (Object[] c : cases) {
			SystemOneResponse r = fake((String) c[0], (Double) c[1], 0.1);
			System.out.println(c[0] + " @ " + c[1] + " (threshold " + threshold + ") -> " + route(r, threshold));
		}
	}
}
