/**
 * Import-specific logging functions for CLEO system.
 * Ported from lib/data/import-logging.sh
 *
 * Provides structured audit logging for import operations including
 * start, success, error, and conflict events.
 *
 * @task T4552
 * @epic T4545
 */
import { getLogPath } from '../paths.js';
import { appendJsonl, readJson } from './json.js';
/**
 * Generate a log entry ID.
 * @task T4552
 */
function generateLogId() {
    const epoch = Math.floor(Date.now() / 1000);
    const rand = Math.random().toString(36).slice(2, 8);
    return `log-${epoch}-${rand}`;
}
/**
 * Extract package metadata from an export file.
 * @task T4552
 */
export async function extractPackageMeta(sourceFilePath) {
    const data = await readJson(sourceFilePath);
    const meta = data?._meta;
    return {
        sourceFile: sourceFilePath.split('/').pop() ?? sourceFilePath,
        sourceProject: meta?.source?.project ?? 'unknown',
        exportedAt: meta?.exportedAt ?? 'unknown',
        packageChecksum: meta?.checksum ?? 'unknown',
        taskCount: typeof meta?.taskCount === 'number' ? meta.taskCount : 0,
    };
}
/**
 * Write a log entry to the log file.
 * @task T4552
 */
async function writeLogEntry(action, taskId, details, sessionId, cwd) {
    const logPath = getLogPath(cwd);
    const entry = {
        id: generateLogId(),
        timestamp: new Date().toISOString(),
        action,
        taskId,
        actor: 'system',
        details,
        sessionId: sessionId ?? null,
    };
    try {
        await appendJsonl(logPath, entry);
    }
    catch {
        // Log failure is non-fatal
    }
}
/**
 * Log import operation start with package metadata.
 * @task T4552
 */
export async function logImportStart(sourceFilePath, sessionId, cwd) {
    let meta;
    try {
        meta = await extractPackageMeta(sourceFilePath);
    }
    catch {
        meta = {
            sourceFile: sourceFilePath.split('/').pop() ?? sourceFilePath,
            sourceProject: 'unknown',
            exportedAt: 'unknown',
            packageChecksum: 'unknown',
            taskCount: 0,
        };
    }
    await writeLogEntry('import', null, {
        ...meta,
        stage: 'start',
    }, sessionId, cwd);
}
/**
 * Log import operation completion with full metadata.
 * @task T4552
 */
export async function logImportSuccess(sourceFilePath, tasksImported, idRemap, conflicts, options, sessionId, cwd) {
    let meta;
    try {
        meta = await extractPackageMeta(sourceFilePath);
    }
    catch {
        meta = {
            sourceFile: sourceFilePath.split('/').pop() ?? sourceFilePath,
            sourceProject: 'unknown',
            exportedAt: 'unknown',
            packageChecksum: 'unknown',
            taskCount: 0,
        };
    }
    await writeLogEntry('import', null, {
        sourceFile: meta.sourceFile,
        sourceProject: meta.sourceProject,
        exportedAt: meta.exportedAt,
        packageChecksum: meta.packageChecksum,
        importedAt: new Date().toISOString(),
        tasksImported,
        idRemap,
        conflicts: conflicts ?? [],
        options: options ?? {},
        stage: 'success',
    }, sessionId, cwd);
}
/**
 * Log import operation error with diagnostic details.
 * @task T4552
 */
export async function logImportError(sourceFilePath, errorMessage, errorCode, stage = 'unknown', sessionId, cwd) {
    await writeLogEntry('error_occurred', null, {
        sourceFile: sourceFilePath.split('/').pop() ?? sourceFilePath,
        stage,
        error: {
            message: errorMessage,
            code: String(errorCode),
            timestamp: new Date().toISOString(),
        },
    }, sessionId, cwd);
}
/**
 * Log import conflict detection and resolution.
 * @task T4552
 */
export async function logImportConflict(conflictType, taskId, conflictDetails, resolution, sessionId, cwd) {
    await writeLogEntry('task_updated', taskId, {
        conflictType,
        resolution,
        details: conflictDetails,
    }, sessionId, cwd);
}
//# sourceMappingURL=import-logging.js.map