import { generateDistinctColors } from "../utils/colors.js";

//#region src/constants/context-breakdown.ts
/**
* Context breakdown schema definitions.
*
* This module defines the breakdown components tracked for each system prompt version.
* It serves as the single source of truth for:
* - Component keys and labels
* - OpenTelemetry span attribute keys
* - UI visualization colors (auto-generated)
*/
/**
* Base schema without colors - colors are auto-generated based on index
*/
const V1_BREAKDOWN_SCHEMA_BASE = [
	{
		key: "systemPromptTemplate",
		label: "System Prompt Template",
		spanAttribute: "context.breakdown.system_template_tokens"
	},
	{
		key: "coreInstructions",
		label: "Core Instructions",
		spanAttribute: "context.breakdown.core_instructions_tokens"
	},
	{
		key: "agentPrompt",
		label: "Agent Prompt",
		spanAttribute: "context.breakdown.agent_prompt_tokens"
	},
	{
		key: "appPrompt",
		label: "App Prompt",
		spanAttribute: "context.breakdown.app_prompt_tokens"
	},
	{
		key: "toolsSection",
		label: "Tools (MCP/Function/Relation)",
		spanAttribute: "context.breakdown.tools_tokens"
	},
	{
		key: "artifactsSection",
		label: "Artifacts",
		spanAttribute: "context.breakdown.artifacts_tokens"
	},
	{
		key: "dataComponents",
		label: "Data Components",
		spanAttribute: "context.breakdown.data_components_tokens"
	},
	{
		key: "artifactComponents",
		label: "Artifact Components",
		spanAttribute: "context.breakdown.artifact_components_tokens"
	},
	{
		key: "transferInstructions",
		label: "Transfer Instructions",
		spanAttribute: "context.breakdown.transfer_instructions_tokens"
	},
	{
		key: "delegationInstructions",
		label: "Delegation Instructions",
		spanAttribute: "context.breakdown.delegation_instructions_tokens"
	},
	{
		key: "thinkingPreparation",
		label: "Thinking Preparation",
		spanAttribute: "context.breakdown.thinking_preparation_tokens"
	},
	{
		key: "conversationHistory",
		label: "Conversation History",
		spanAttribute: "context.breakdown.conversation_history_tokens"
	}
];
/** Pre-generated distinct colors for the schema */
const SCHEMA_COLORS = generateDistinctColors(V1_BREAKDOWN_SCHEMA_BASE.length);
/**
* V1 Breakdown Schema - defines all context components tracked for V1 system prompts.
* Colors are automatically generated for visual distinction.
*/
const V1_BREAKDOWN_SCHEMA = V1_BREAKDOWN_SCHEMA_BASE.map((def, index) => ({
	...def,
	color: SCHEMA_COLORS[index]
}));
/** Span attribute key for total tokens */
const CONTEXT_BREAKDOWN_TOTAL_SPAN_ATTRIBUTE = "context.breakdown.total_tokens";
/**
* Creates an empty context breakdown from a schema definition.
* @param schema - Array of breakdown component definitions
*/
function createEmptyBreakdown(schema) {
	const components = {};
	for (const def of schema) components[def.key] = 0;
	return {
		components,
		total: 0
	};
}
/**
* Calculates the total from all breakdown components and updates the total field.
* @param breakdown - The context breakdown to calculate total for
* @returns The breakdown with updated total
*/
function calculateBreakdownTotal(breakdown) {
	breakdown.total = Object.values(breakdown.components).reduce((sum, val) => sum + val, 0);
	return breakdown;
}
/**
* Parses a context breakdown from span attributes using a schema.
* @param data - Span attribute data record
* @param schema - Breakdown schema to use for parsing
* @returns Parsed context breakdown
*/
function parseContextBreakdownFromSpan(data, schema = V1_BREAKDOWN_SCHEMA) {
	const breakdown = createEmptyBreakdown(schema);
	for (const def of schema) {
		const value = data[def.spanAttribute];
		breakdown.components[def.key] = typeof value === "number" ? value : Number(value) || 0;
	}
	const totalValue = data[CONTEXT_BREAKDOWN_TOTAL_SPAN_ATTRIBUTE];
	breakdown.total = typeof totalValue === "number" ? totalValue : Number(totalValue) || 0;
	return breakdown;
}

//#endregion
export { CONTEXT_BREAKDOWN_TOTAL_SPAN_ATTRIBUTE, V1_BREAKDOWN_SCHEMA, calculateBreakdownTotal, createEmptyBreakdown, parseContextBreakdownFromSpan };