import { appendFileSync, chmodSync, mkdirSync } from 'fs';
import { dirname } from 'path';
import { getReviewCopilotConfig } from '../../cloud/config.js';
function telemetryPath() {
    return getReviewCopilotConfig().telemetryPath;
}
export function appendReviewCopilotTelemetry(event) {
    const path = telemetryPath();
    try {
        mkdirSync(dirname(path), { recursive: true });
        appendFileSync(path, JSON.stringify(event) + '\n', { mode: 0o600 });
        try {
            chmodSync(path, 0o600);
        }
        catch { /* best-effort */ }
    }
    catch {
        // Telemetry must never affect the quarantine review flow.
    }
}
export function recordAnnotationCreated(annotation) {
    appendReviewCopilotTelemetry({
        type: 'annotation_created',
        ts: new Date().toISOString(),
        item_id: annotation.itemId,
        category: annotation.category,
        suggested_action: annotation.suggestedAction,
        confidence: annotation.confidence,
        similar_group_key: annotation.similarGroupKey,
        copilot_version: annotation.copilotVersion,
    });
}
export function recordOperatorDecision(itemId, decision, reviewedBy) {
    if (!getReviewCopilotConfig().enabled)
        return;
    appendReviewCopilotTelemetry({
        type: 'operator_decision',
        ts: new Date().toISOString(),
        item_id: itemId,
        decision,
        reviewed_by: reviewedBy,
    });
}
