import assert from "node:assert/strict";
import { test, describe } from 'vitest';

import {
	formatServiceTierFooterStatus,
	formatServiceTierStatus,
	resolveServiceTierIcon,
	supportsServiceTier,
} from "../service-tier.ts";

// ─── supportsServiceTier ─────────────────────────────────────────────────────

describe("supportsServiceTier", () => {
	test("returns true for gpt-5.4", () => {
		assert.equal(supportsServiceTier("gpt-5.4"), true);
	});

	test("returns true for gpt-5.4-pro", () => {
		assert.equal(supportsServiceTier("gpt-5.4-pro"), true);
	});

	test("returns true for gpt-5.4-mini", () => {
		assert.equal(supportsServiceTier("gpt-5.4-mini"), true);
	});

	test("returns true for openai/gpt-5.4 (provider-prefixed)", () => {
		assert.equal(supportsServiceTier("openai/gpt-5.4"), true);
	});

	test("returns true for vibeproxy-openai/gpt-5.4 (proxy provider-prefixed)", () => {
		assert.equal(supportsServiceTier("vibeproxy-openai/gpt-5.4"), true);
	});

	test("returns false for provider-only identifier without gpt-5.4 model suffix", () => {
		assert.equal(supportsServiceTier("vibeproxy-openai"), false);
	});

	test("returns false for claude-opus-4-6", () => {
		assert.equal(supportsServiceTier("claude-opus-4-6"), false);
	});

	test("returns false for gemini-2.5-pro", () => {
		assert.equal(supportsServiceTier("gemini-2.5-pro"), false);
	});

	test("returns false for gpt-4o", () => {
		assert.equal(supportsServiceTier("gpt-4o"), false);
	});

	test("returns false for empty string", () => {
		assert.equal(supportsServiceTier(""), false);
	});
});

// ─── formatServiceTierStatus ─────────────────────────────────────────────────

describe("formatServiceTierStatus", () => {
	test("shows disabled when service_tier is undefined", () => {
		const output = formatServiceTierStatus(undefined);
		assert.ok(output.includes("disabled"), `Expected 'disabled' in: ${output}`);
	});

	test("mentions provider-agnostic model gating", () => {
		const output = formatServiceTierStatus("priority");
		assert.ok(
			output.includes("regardless of provider"),
			`Expected provider note in: ${output}`,
		);
	});

	test("shows priority when set to priority", () => {
		const output = formatServiceTierStatus("priority");
		assert.ok(output.includes("priority"), `Expected 'priority' in: ${output}`);
	});

	test("shows flex when set to flex", () => {
		const output = formatServiceTierStatus("flex");
		assert.ok(output.includes("flex"), `Expected 'flex' in: ${output}`);
	});
});

// ─── formatServiceTierFooterStatus ───────────────────────────────────────────

describe("formatServiceTierFooterStatus", () => {
	test("returns priority footer status for supported model", () => {
		assert.equal(
			formatServiceTierFooterStatus("priority", "vibeproxy-openai/gpt-5.4"),
			"fast: ⚡ priority",
		);
	});

	test("returns undefined for unsupported model", () => {
		assert.equal(
			formatServiceTierFooterStatus("priority", "claude-opus-4-6"),
			undefined,
		);
	});

	test("returns undefined when tier is disabled", () => {
		assert.equal(
			formatServiceTierFooterStatus(undefined, "gpt-5.4"),
			undefined,
		);
	});
});

// ─── resolveServiceTierIcon ──────────────────────────────────────────────────

describe("resolveServiceTierIcon", () => {
	test("returns lightning bolt for priority tier on supported model", () => {
		const icon = resolveServiceTierIcon("priority", "gpt-5.4");
		assert.equal(icon, "⚡");
	});

	test("returns money icon for flex tier on supported model", () => {
		const icon = resolveServiceTierIcon("flex", "gpt-5.4");
		assert.equal(icon, "💰");
	});

	test("returns empty string when tier is set but model does not support it", () => {
		const icon = resolveServiceTierIcon("priority", "claude-opus-4-6");
		assert.equal(icon, "");
	});

	test("returns empty string when tier is undefined", () => {
		const icon = resolveServiceTierIcon(undefined, "gpt-5.4");
		assert.equal(icon, "");
	});

	test("returns empty string when both tier and model are unsupported", () => {
		const icon = resolveServiceTierIcon(undefined, "claude-opus-4-6");
		assert.equal(icon, "");
	});

	test("returns empty string when model is empty", () => {
		const icon = resolveServiceTierIcon("priority", "");
		assert.equal(icon, "");
	});
});

// ─── Disable via "off" preference ────────────────────────────────────────────
//
// These are structural tests against the source so they don't depend on the
// effective-preferences loader behaving the same way in a test env as in a
// real SF install. The runtime behavior is covered by hook integration paths.

import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const serviceTierSrc = readFileSync(
	join(__dirname, "..", "service-tier.ts"),
	"utf-8",
);
const hooksSrc = readFileSync(
	join(__dirname, "..", "bootstrap", "register-hooks.ts"),
	"utf-8",
);

describe('service_tier: "off" disable', () => {
	test("isServiceTierDisabled is exported and keys off the preference", () => {
		assert.match(serviceTierSrc, /export function isServiceTierDisabled\(\)/);
		assert.match(
			serviceTierSrc,
			/isServiceTierDisabled[\s\S]*?prefs\?\.service_tier[\s\S]*?===\s*"off"/,
		);
	});

	test('getEffectiveServiceTier does not treat "off" as a tier', () => {
		// getEffectiveServiceTier only returns priority|flex — "off" collapses to undefined.
		assert.match(
			serviceTierSrc,
			/export function getEffectiveServiceTier\(\)[\s\S]*?if \(raw === "priority" \|\| raw === "flex"\) return raw;\s*return undefined;/,
		);
	});

	test("syncServiceTierStatus skips setStatus when disabled", () => {
		assert.match(
			hooksSrc,
			/async function syncServiceTierStatus\([\s\S]*?if \(isServiceTierDisabled\(\)\) return;[\s\S]*?setStatus\("sf-fast"/,
		);
	});

	test("before_provider_request hook short-circuits when disabled", () => {
		// Must check isServiceTierDisabled before the normal tier/support gate.
		const hook = hooksSrc.slice(hooksSrc.indexOf("// ── Service Tier ──"));
		assert.match(hook, /if \(isServiceTierDisabled\(\)\) return payload/);
		assert.ok(
			hook.indexOf("isServiceTierDisabled") <
				hook.indexOf("getEffectiveServiceTier()"),
			"disable check must run before tier resolution",
		);
	});

	test("/sf fast on refuses when explicitly disabled", () => {
		// handleFast("on") must bail out with a warning rather than silently
		// overwriting the explicit "off" pref.
		assert.match(
			serviceTierSrc,
			/if \(trimmed === "on" \|\| trimmed === "flex"\)[\s\S]*?if \(isServiceTierDisabled\(\)\)[\s\S]*?"warning"/,
		);
	});

	test('preferences-types declares "off" as a valid service_tier value', () => {
		const typesSrc = readFileSync(
			join(__dirname, "..", "preferences-types.ts"),
			"utf-8",
		);
		assert.match(
			typesSrc,
			/service_tier\?:\s*"priority"\s*\|\s*"flex"\s*\|\s*"off"/,
		);
	});
});
