import type { AssistantMessage } from "@mariozechner/pi-ai";
import type { ContextUsage } from "@mariozechner/pi-coding-agent";

export type ISummaryThinkingLevel = "off" | "low" | "medium" | "high";

export type IPolicyKey =
	| "trigger.maxTokens"
	| "trigger.minTokens"
	| "trigger.cooldownMs"
	| "trigger.builtinReserveTokens"
	| "trigger.builtinSkipMarginPercent"
	| "ui.name"
	| "ui.quiet"
	| "ui.showStatus"
	| "ui.minimalStatus"
	| "summary.thinkingLevel"
	| "summary.preservationInstruction";

export interface ISummaryModelOverride {
	thinkingLevel?: ISummaryThinkingLevel;
	preservationInstruction?: string;
}

export interface IModelEntry extends ISummaryModelOverride {
	model: string;
}

export interface ISummaryPolicy {
	thinkingLevel: ISummaryThinkingLevel;
	preservationInstruction: string;
}

export interface ICompactionPolicy {
	enabled: boolean;
	trigger: {
		maxTokens?: number;
		minTokens: number;
		cooldownMs: number;
		builtinReserveTokens: number;
		builtinSkipMarginPercent: number;
	};
	models: IModelEntry[];
	ui: {
		name: string;
		quiet: boolean;
		showStatus: boolean;
		minimalStatus: boolean;
	};
	summary: ISummaryPolicy;
	profiles?: Record<string, IProfileOverride>;
}

export interface ICompactionPolicyPatch {
	enabled?: boolean;
	trigger?: Partial<ICompactionPolicy["trigger"]>;
	models?: IModelEntry[];
	ui?: Partial<ICompactionPolicy["ui"]>;
	summary?: Partial<ICompactionPolicy["summary"]>;
	profiles?: Record<string, IProfileOverride>;
}

export type IParseResult<T> = { ok: true; value: T } | { ok: false; error: string };

export interface IProactiveTriggerInput {
	lastAssistantMessage: AssistantMessage | undefined;
	usage: ContextUsage | undefined;
	inFlight: boolean;
	nowMs: number;
	lastProactiveAtMs: number | undefined;
	policy: ICompactionPolicy;
}

export interface ITemplateResolution {
	template?: string;
	updateTemplate?: string;
	resolvedPath?: string;
	updateResolvedPath?: string;
	fallbackReason?: string;
	updateFallbackReason?: string;
}

export interface IProfileOverride {
	match: string;
	trigger?: Partial<ICompactionPolicy["trigger"]>;
	models?: IModelEntry[];
	summary?: ISummaryModelOverride;
	template?: string;
	updateTemplate?: string;
}

export interface ICompactionDetails {
	readFiles: string[];
	modifiedFiles: string[];
}

export const CONFIG_FILE = ".pi/compaction-policy.json";

export const DEFAULT_POLICY: ICompactionPolicy = {
  enabled: false,
  trigger: {
    minTokens: 100_000,
    cooldownMs: 60_000,
    builtinReserveTokens: 16_384,
    builtinSkipMarginPercent: 5,
  },
  models: [{ model: "openai-codex/gpt-5.3-codex" }],
  ui: {
    name: "compact",
    quiet: false,
    showStatus: true,
    minimalStatus: false,
  },
  summary: {
    thinkingLevel: "low",
    preservationInstruction: "Preserve exact file paths, function names, and error messages.",
  },
};

export const POLICY_KEYS: IPolicyKey[] = [
  "trigger.maxTokens",
  "trigger.minTokens",
  "trigger.cooldownMs",
  "trigger.builtinReserveTokens",
  "trigger.builtinSkipMarginPercent",
  "ui.name",
  "ui.quiet",
  "ui.showStatus",
  "ui.minimalStatus",
  "summary.thinkingLevel",
  "summary.preservationInstruction",
];
