import fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import crypto from "node:crypto";
import { encodeCwd } from "./core/encode-cwd.js";
const SESSION_DIR = path.join(os.homedir(), ".ezcoder", "sessions");
function sessionDirForCwd(cwd) {
    return path.join(SESSION_DIR, encodeCwd(cwd));
}
export async function createSession(cwd, provider, model) {
    const id = crypto.randomUUID();
    const timestamp = new Date().toISOString();
    const dir = sessionDirForCwd(cwd);
    await fs.mkdir(dir, { recursive: true });
    const fileName = `${timestamp.replace(/[:.]/g, "-")}_${id.slice(0, 8)}.jsonl`;
    const filePath = path.join(dir, fileName);
    const header = {
        type: "session",
        version: 1,
        id,
        timestamp,
        cwd,
        provider: provider,
        model,
    };
    await fs.appendFile(filePath, JSON.stringify(header) + "\n", "utf-8");
    return {
        id,
        path: filePath,
        async append(entry) {
            await fs.appendFile(filePath, JSON.stringify(entry) + "\n", "utf-8");
        },
    };
}
// ── Load Session ────────────────────────────────────────────
export async function loadSession(sessionPath) {
    const content = await fs.readFile(sessionPath, "utf-8");
    const lines = content.trim().split("\n").filter(Boolean);
    let header = null;
    const messages = [];
    for (const line of lines) {
        const entry = JSON.parse(line);
        if (entry.type === "session") {
            header = entry;
        }
        else if (entry.type === "message") {
            // Skip system messages — they'll be rebuilt fresh
            if (entry.message.role !== "system") {
                messages.push(entry.message);
            }
        }
    }
    if (!header) {
        throw new Error(`Invalid session file: no header found in ${sessionPath}`);
    }
    return { header, messages };
}
// ── List Sessions ───────────────────────────────────────────
export async function listSessions(cwd) {
    const dir = sessionDirForCwd(cwd);
    let files;
    try {
        files = await fs.readdir(dir);
    }
    catch {
        return [];
    }
    const sessions = [];
    for (const file of files) {
        if (!file.endsWith(".jsonl"))
            continue;
        const filePath = path.join(dir, file);
        try {
            const content = await fs.readFile(filePath, "utf-8");
            const lines = content.trim().split("\n").filter(Boolean);
            if (lines.length === 0)
                continue;
            const header = JSON.parse(lines[0]);
            if (header.type !== "session")
                continue;
            let messageCount = 0;
            let lastActivity = header.timestamp;
            for (const line of lines) {
                try {
                    const entry = JSON.parse(line);
                    if (entry.type === "message") {
                        messageCount++;
                        if (entry.timestamp)
                            lastActivity = entry.timestamp;
                    }
                }
                catch {
                    // Skip corrupt lines
                }
            }
            sessions.push({
                id: header.id,
                path: filePath,
                timestamp: header.timestamp,
                lastActivity,
                cwd: header.cwd,
                messageCount,
            });
        }
        catch {
            // Skip corrupt files
        }
    }
    // Sort by last activity descending (the session most recently spoken in first)
    sessions.sort((a, b) => b.lastActivity.localeCompare(a.lastActivity));
    return sessions;
}
// ── Get Most Recent Session ─────────────────────────────────
export async function getMostRecentSession(cwd) {
    const sessions = await listSessions(cwd);
    return sessions.length > 0 ? sessions[0].path : null;
}
// ── Persist Messages ────────────────────────────────────────
export function persistMessage(session, message) {
    const entry = {
        type: "message",
        timestamp: new Date().toISOString(),
        message,
    };
    return session.append(entry);
}
//# sourceMappingURL=session.js.map