import os from 'node:os';
import path from 'node:path';
import * as fsp from 'node:fs/promises';
function mapEndpointToFolder(ep) {
    const e = String(ep || '').trim().toLowerCase();
    if (e.includes('/v1/responses') || e.includes('/responses.submit'))
        return 'openai-responses';
    if (e.includes('/v1/messages'))
        return 'anthropic-messages';
    return 'openai-chat';
}
function isSnapshotEnabled() {
    const v = String(process?.env?.RCC_FILTER_SNAPSHOT || process?.env?.RCC_HOOKS_VERBOSITY || '').toLowerCase();
    return v === '1' || v === 'true' || v === 'verbose';
}
function sanitizeToken(value, fallback) {
    if (typeof value !== 'string') {
        return fallback;
    }
    const trimmed = value.trim();
    if (!trimmed) {
        return fallback;
    }
    return trimmed.replace(/[^A-Za-z0-9_.-]/g, '_') || fallback;
}
function toErrorCode(error) {
    if (!error || typeof error !== 'object') {
        return undefined;
    }
    const code = error.code;
    return typeof code === 'string' && code.trim() ? code : undefined;
}
async function writeUniqueFile(dir, baseName, contents) {
    const parsed = path.parse(baseName);
    const ext = parsed.ext || '.json';
    const stem = parsed.name || 'snapshot';
    for (let i = 0; i < 64; i += 1) {
        const name = i === 0 ? `${stem}${ext}` : `${stem}_${i}${ext}`;
        try {
            await fsp.writeFile(path.join(dir, name), contents, { encoding: 'utf-8', flag: 'wx' });
            return;
        }
        catch (error) {
            if (toErrorCode(error) === 'EEXIST') {
                continue;
            }
            throw error;
        }
    }
    const fallback = `${stem}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}${ext}`;
    await fsp.writeFile(path.join(dir, fallback), contents, 'utf-8');
}
export async function writeFilterSnapshot(options) {
    try {
        if (!isSnapshotEnabled())
            return;
        const rid = sanitizeToken(options.requestId || '', `req_${Date.now()}`);
        const baseOverride = process?.env?.RCC_SNAPSHOT_DIR;
        const base = baseOverride && baseOverride.trim()
            ? baseOverride.trim()
            : path.join(os.homedir(), '.routecodex', 'codex-samples');
        const folder = mapEndpointToFolder(options.endpoint);
        const provider = sanitizeToken(options.profile || '', '__pending__');
        const dir = path.join(base, folder, provider, rid);
        await fsp.mkdir(dir, { recursive: true });
        const parts = ['filters', options.stage.replace(/\s+/g, ''), options.tag || (options.name ? `after_${options.name}` : 'after')]
            .filter(Boolean)
            .join('_');
        const file = `${sanitizeToken(parts, 'filters')}.json`;
        const payload = {
            meta: {
                requestId: rid,
                stage: options.stage,
                name: options.name,
                tag: options.tag,
                profile: options.profile,
                endpoint: options.endpoint,
                ts: new Date().toISOString()
            },
            data: options.data
        };
        await writeUniqueFile(dir, file, JSON.stringify(payload, null, 2));
    }
    catch { /* ignore snapshot errors */ }
}
