/**
 * Evidence recording for RCASD lifecycle stages.
 *
 * Records provenance evidence (files, URLs, manifests) linked to
 * lifecycle stages in SQLite.
 *
 * @task T5200
 * @epic T4798
 */
import { basename, relative } from 'node:path';
import { getLogger } from '../logger.js';
import { getCleoDirAbsolute } from '../paths.js';
import * as schema from '../store/tasks-schema.js';
const log = getLogger('lifecycle:evidence');
// =============================================================================
// RECORD EVIDENCE
// =============================================================================
/**
 * Record an evidence artifact linked to a lifecycle stage.
 *
 * Writes to the SQLite `lifecycle_evidence` table.
 *
 * @param epicId - Epic task ID (e.g. 'T4881')
 * @param stage - Canonical stage name (e.g. 'research')
 * @param uri - URI of the evidence artifact
 * @param type - Evidence type: 'file', 'url', or 'manifest'
 * @param options - Optional agent and description
 * @returns The created evidence record
 */
export async function recordEvidence(epicId, stage, uri, type, options) {
    const now = new Date().toISOString();
    const stageId = `stage-${epicId}-${stage}`;
    const id = `evidence-${epicId}-${stage}-${Date.now()}`;
    const record = {
        id,
        stageId,
        uri,
        type,
        recordedAt: now,
        recordedBy: options?.agent,
        description: options?.description,
    };
    // SQLite write (best-effort)
    try {
        const { getDb } = await import('../store/sqlite.js');
        const db = await getDb(options?.cwd);
        await db
            .insert(schema.lifecycleEvidence)
            .values({
            id,
            stageId,
            uri,
            type,
            recordedAt: now,
            recordedBy: options?.agent ?? null,
            description: options?.description ?? null,
        })
            .run();
    }
    catch (err) {
        log.warn({ err }, 'Failed to write evidence to SQLite');
    }
    return record;
}
// =============================================================================
// GET EVIDENCE
// =============================================================================
/**
 * Query evidence records for an epic, optionally filtered by stage.
 *
 * @param epicId - Epic task ID
 * @param stage - Optional stage name filter
 * @param cwd - Optional working directory
 * @returns Array of evidence records
 */
export async function getEvidence(epicId, stage, cwd) {
    try {
        const { getDb } = await import('../store/sqlite.js');
        const { eq, like } = await import('drizzle-orm');
        const db = await getDb(cwd);
        let rows;
        if (stage) {
            const stageId = `stage-${epicId}-${stage}`;
            rows = await db
                .select()
                .from(schema.lifecycleEvidence)
                .where(eq(schema.lifecycleEvidence.stageId, stageId))
                .all();
        }
        else {
            const stageIdPrefix = `stage-${epicId}-`;
            rows = await db
                .select()
                .from(schema.lifecycleEvidence)
                .where(like(schema.lifecycleEvidence.stageId, `${stageIdPrefix}%`))
                .all();
        }
        return rows.map((row) => ({
            id: row.id,
            stageId: row.stageId,
            uri: row.uri,
            type: row.type,
            recordedAt: row.recordedAt,
            recordedBy: row.recordedBy ?? undefined,
            description: row.description ?? undefined,
        }));
    }
    catch (err) {
        log.warn({ err }, 'Failed to query evidence');
        return [];
    }
}
// =============================================================================
// LINK PROVENANCE
// =============================================================================
/**
 * Convenience wrapper to record a file as provenance evidence.
 *
 * Converts the file path to a URI relative to the `.cleo/` directory,
 * sets the type to 'file', and extracts a description from the filename.
 *
 * @param epicId - Epic task ID
 * @param stage - Canonical stage name
 * @param filePath - Absolute or relative path to the file
 * @param cwd - Optional working directory
 * @returns The created evidence record
 */
export async function linkProvenance(epicId, stage, filePath, cwd) {
    const cleoDir = getCleoDirAbsolute(cwd);
    const relativeUri = relative(cleoDir, filePath).replaceAll('\\', '/');
    const description = basename(filePath);
    return recordEvidence(epicId, stage, relativeUri, 'file', {
        description,
        cwd,
    });
}
// =============================================================================
// EVIDENCE SUMMARY
// =============================================================================
/**
 * Aggregate evidence counts per stage for an epic.
 *
 * @param epicId - Epic task ID
 * @param cwd - Optional working directory
 * @returns Array of per-stage summaries with type breakdowns
 */
export async function getEvidenceSummary(epicId, cwd) {
    try {
        const { getDb } = await import('../store/sqlite.js');
        const { like } = await import('drizzle-orm');
        const db = await getDb(cwd);
        const stageIdPrefix = `stage-${epicId}-`;
        const rows = await db
            .select()
            .from(schema.lifecycleEvidence)
            .where(like(schema.lifecycleEvidence.stageId, `${stageIdPrefix}%`))
            .all();
        // Group by stage
        const stageMap = new Map();
        for (const row of rows) {
            const stage = row.stageId.replace(stageIdPrefix, '');
            if (!stageMap.has(stage)) {
                stageMap.set(stage, { count: 0, types: { file: 0, url: 0, manifest: 0 } });
            }
            const entry = stageMap.get(stage);
            entry.count++;
            entry.types[row.type]++;
        }
        return Array.from(stageMap.entries()).map(([stage, data]) => ({
            stage,
            count: data.count,
            types: data.types,
        }));
    }
    catch (err) {
        log.warn({ err }, 'Failed to get evidence summary');
        return [];
    }
}
//# sourceMappingURL=evidence.js.map