import { deriveActualsFromGit } from './git-analyzer.js';
import { consolidateActuals } from './consolidator.js';
const ELIGIBLE_STATUSES = new Set(['implementing', 'done']);
/**
 * Backfills actuals for specs that have no actuals yet, by deriving from git history.
 *
 * This function does NOT persist to spec-store — it returns the report and derived data.
 * The caller decides whether to persist.
 */
export async function backfillActualsFromGit(projectPath, _projectDataDir, specs) {
    const report = {
        totalSpecs: specs.length,
        populated: 0,
        skipped: 0,
        noGitData: 0,
        alreadyHadActuals: 0,
        totalDerivedHours: 0,
        results: [],
    };
    for (const spec of specs) {
        if (!ELIGIBLE_STATUSES.has(spec.status)) {
            report.skipped++;
            continue;
        }
        if (spec.actuals !== null) {
            report.alreadyHadActuals++;
            continue;
        }
        const gitDerived = await deriveActualsFromGit(projectPath, spec.id);
        if (!gitDerived) {
            report.noGitData++;
            continue;
        }
        const hourlyRate = spec.estimation.hourlyRate;
        const consolidated = consolidateActuals({ hourlyRate }, null, null, gitDerived);
        /* v8 ignore start */
        if (!consolidated) {
            report.noGitData++;
            continue;
        }
        /* v8 ignore stop */
        report.populated++;
        report.totalDerivedHours += gitDerived.devHours;
        const result = {
            specId: spec.id,
            actuals: consolidated,
        };
        report.results.push(result);
    }
    return report;
}
/**
 * Derives actuals for a single spec and returns a ConsolidatedActuals or null.
 */
export async function deriveAndConsolidateSingle(projectPath, spec) {
    const gitDerived = await deriveActualsFromGit(projectPath, spec.id);
    if (!gitDerived) {
        return null;
    }
    return consolidateActuals({ hourlyRate: spec.estimation.hourlyRate }, null, null, gitDerived);
}
//# sourceMappingURL=backfill.js.map