import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, test } from "vitest";

import { loadGatewayConfigFromEnv } from "../memory-embeddings.js";

const ENV_KEYS = [
	"SF_LLM_GATEWAY_EMBED_MODEL",
	"SF_LLM_GATEWAY_RERANK_MODEL",
	"SF_LLM_GATEWAY_EMBED_QUERY_INSTRUCTION",
];

let originalHome;
let tmpHome;

beforeEach(() => {
	originalHome = process.env.HOME;
	tmpHome = mkdtempSync(join(tmpdir(), "sf-test-"));
	mkdirSync(join(tmpHome, ".sf", "agent"), { recursive: true });
	process.env.HOME = tmpHome;
});

afterEach(() => {
	process.env.HOME = originalHome;
	for (const key of ENV_KEYS) delete process.env[key];
});

function writeAuthJson(entry) {
	const authPath = join(tmpHome, ".sf", "agent", "auth.json");
	writeFileSync(authPath, JSON.stringify({ "llm-gateway": entry }));
}

test("loadGatewayConfigFromEnv returns null when auth.json has no llm-gateway entry", () => {
	const authPath = join(tmpHome, ".sf", "agent", "auth.json");
	writeFileSync(authPath, JSON.stringify({}));
	assert.equal(loadGatewayConfigFromEnv(), null);
});

test("loadGatewayConfigFromEnv returns null when auth.json does not exist", () => {
	assert.equal(loadGatewayConfigFromEnv(), null);
});

test("loadGatewayConfigFromEnv reads key and url from auth.json", () => {
	writeAuthJson({
		key: "auth-key",
		url: "https://example.test/v1",
		type: "api_key",
	});

	const cfg = loadGatewayConfigFromEnv();
	assert.equal(cfg.apiKey, "auth-key");
	assert.equal(cfg.url, "https://example.test/v1");
	assert.equal(cfg.keySource, "auth.json:llm-gateway");
	assert.equal(cfg.urlSource, "auth.json:llm-gateway");
	assert.equal(cfg.embeddingModel, "Qwen/Qwen3-Embedding-4B");
	assert.equal(cfg.rerankModel, "Qwen/Qwen3-Reranker-0.6B");
});

test("loadGatewayConfigFromEnv uses default url when auth.json has no url", () => {
	writeAuthJson({ key: "auth-key", type: "api_key" });

	const cfg = loadGatewayConfigFromEnv();
	assert.equal(cfg.url, "https://llm-gateway.centralcloud.com/v1");
	assert.equal(cfg.urlSource, "default");
});

test("loadGatewayConfigFromEnv respects model env var overrides", () => {
	writeAuthJson({ key: "auth-key", type: "api_key" });
	process.env.SF_LLM_GATEWAY_EMBED_MODEL = "custom-embed";
	process.env.SF_LLM_GATEWAY_RERANK_MODEL = "custom-rerank";

	const cfg = loadGatewayConfigFromEnv();
	assert.equal(cfg.embeddingModel, "custom-embed");
	assert.equal(cfg.rerankModel, "custom-rerank");
});
