import { i as writeTextAtomic } from "./json-files-kvH2Eboc.js";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { mkdir, readFile, readdir, stat } from "node:fs/promises";
//#region src/cognitive/insight/store.ts
const DEFAULT_INSIGHT_TTL_DAYS = 60;
const MAX_INSIGHTS_PER_USER = 200;
const INSIGHT_STORE_VERSION = 1;
var InsightStore = class {
	constructor(configDir) {
		this.configDir = configDir;
	}
	insightsDir(agentId) {
		return join(this.configDir, "cognitive", "insights", agentId);
	}
	recordPath(agentId, userId) {
		return join(this.insightsDir(agentId), `${userId}.json`);
	}
	async loadRecords(agentId, userId) {
		const path = this.recordPath(agentId, userId);
		if (!existsSync(path)) return [];
		try {
			const raw = await readFile(path, "utf-8");
			const data = JSON.parse(raw);
			return Array.isArray(data.insights) ? data.insights : [];
		} catch {
			return [];
		}
	}
	async writeRecords(agentId, userId, records) {
		await mkdir(this.insightsDir(agentId), { recursive: true });
		const data = {
			insights: records.slice(-MAX_INSIGHTS_PER_USER),
			version: INSIGHT_STORE_VERSION
		};
		await writeTextAtomic(this.recordPath(agentId, userId), JSON.stringify(data, null, 2));
	}
	async save(agentId, userId, record) {
		const records = await this.loadRecords(agentId, userId);
		const idx = records.findIndex((r) => r.id === record.id);
		if (idx >= 0) records[idx] = record;
		else records.push(record);
		await this.writeRecords(agentId, userId, records);
	}
	async load(agentId, userId, id) {
		return (await this.loadRecords(agentId, userId)).find((r) => r.id === id);
	}
	async listRecent(agentId, userId, limit) {
		return (await this.loadRecords(agentId, userId)).toSorted((a, b) => b.generatedAt - a.generatedAt).slice(0, limit ?? 20);
	}
	async listActive(agentId, userId, ttlDays, limit) {
		const ttl = (ttlDays ?? DEFAULT_INSIGHT_TTL_DAYS) * 864e5;
		const cutoff = Date.now() - ttl;
		return (await this.loadRecords(agentId, userId)).filter((r) => r.generatedAt >= cutoff).toSorted((a, b) => b.generatedAt - a.generatedAt).slice(0, limit ?? 20);
	}
	async updateFeedback(agentId, userId, id, feedback, userResponse) {
		const records = await this.loadRecords(agentId, userId);
		const record = records.find((r) => r.id === id);
		if (!record) return;
		record.feedback = feedback;
		if (userResponse !== void 0) record.userResponse = userResponse;
		await this.writeRecords(agentId, userId, records);
	}
	async removeStale(agentId, userId, ttlDays) {
		const records = await this.loadRecords(agentId, userId);
		const ttl = (ttlDays ?? DEFAULT_INSIGHT_TTL_DAYS) * 864e5;
		const cutoff = Date.now() - ttl;
		const active = records.filter((r) => r.generatedAt >= cutoff);
		const removed = records.length - active.length;
		if (removed > 0) await this.writeRecords(agentId, userId, active);
		return removed;
	}
	async listUserIds(agentId) {
		const dir = this.insightsDir(agentId);
		try {
			return (await readdir(dir)).filter((name) => name.endsWith(".json")).map((name) => name.slice(0, -5)).toSorted();
		} catch {
			return [];
		}
	}
	async listAgentIds() {
		const dir = join(this.configDir, "cognitive", "insights");
		try {
			const entries = await readdir(dir);
			const result = [];
			for (const name of entries) if ((await stat(join(dir, name))).isDirectory()) result.push(name);
			return result.toSorted();
		} catch {
			return [];
		}
	}
};
//#endregion
export { InsightStore };
