/**
 * Repair functions for fixable data integrity issues — SQLite era.
 *
 * Each function performs a direct Drizzle SQL update (no in-memory TaskFile mutation).
 * Used by both `upgrade` and `validate --fix`.
 *
 * @task T4862
 */
/**
 * Set size='medium' on tasks that have no size value.
 * Operates directly on the SQLite tasks table.
 */
export async function repairMissingSizes(cwd, dryRun) {
    const { getDb } = await import('./store/sqlite.js');
    const { tasks } = await import('./store/tasks-schema.js');
    const { isNull } = await import('drizzle-orm');
    const db = await getDb(cwd);
    const affected = await db.select({ id: tasks.id }).from(tasks).where(isNull(tasks.size));
    if (affected.length === 0) {
        return {
            action: 'fix_missing_sizes',
            status: 'skipped',
            details: 'No tasks missing size field',
        };
    }
    if (dryRun) {
        return {
            action: 'fix_missing_sizes',
            status: 'preview',
            details: `Would set size='medium' for ${affected.length} task(s)`,
        };
    }
    await db.update(tasks).set({ size: 'medium' }).where(isNull(tasks.size));
    return {
        action: 'fix_missing_sizes',
        status: 'applied',
        details: `Set size='medium' for ${affected.length} task(s)`,
    };
}
/**
 * Set completedAt=now() on done/cancelled tasks that are missing a completedAt timestamp.
 * Operates directly on the SQLite tasks table.
 */
export async function repairMissingCompletedAt(cwd, dryRun) {
    const { getDb } = await import('./store/sqlite.js');
    const { tasks } = await import('./store/tasks-schema.js');
    const { sql } = await import('drizzle-orm');
    const db = await getDb(cwd);
    const affected = await db
        .select({ id: tasks.id })
        .from(tasks)
        .where(sql `(${tasks.status} = 'done' OR ${tasks.status} = 'cancelled') AND ${tasks.completedAt} IS NULL`);
    if (affected.length === 0) {
        return {
            action: 'fix_completed_at',
            status: 'skipped',
            details: 'No done/cancelled tasks missing completedAt',
        };
    }
    if (dryRun) {
        return {
            action: 'fix_completed_at',
            status: 'preview',
            details: `Would set completedAt for ${affected.length} done/cancelled task(s)`,
        };
    }
    const now = new Date().toISOString();
    await db
        .update(tasks)
        .set({ completedAt: now })
        .where(sql `(${tasks.status} = 'done' OR ${tasks.status} = 'cancelled') AND ${tasks.completedAt} IS NULL`);
    return {
        action: 'fix_completed_at',
        status: 'applied',
        details: `Set completedAt for ${affected.length} done/cancelled task(s)`,
    };
}
/**
 * Detect and add missing required columns on the tasks table.
 * Uses PRAGMA table_info to check actual schema vs required columns.
 *
 * @see https://github.com/anthropics/cleo/issues/63
 */
export async function repairMissingColumns(cwd, dryRun) {
    const { getDb } = await import('./store/sqlite.js');
    const db = await getDb(cwd);
    // getDb() already calls ensureRequiredColumns(), so if we get here
    // the column should exist. But we still report the action for visibility.
    const { sql } = await import('drizzle-orm');
    const columns = db.all(sql `PRAGMA table_info(tasks)`);
    const existingCols = new Set(columns.map((c) => c.name));
    const missingCols = ['pipeline_stage'].filter((c) => !existingCols.has(c));
    if (missingCols.length === 0) {
        return {
            action: 'fix_missing_columns',
            status: 'skipped',
            details: 'All required columns present on tasks table',
        };
    }
    if (dryRun) {
        return {
            action: 'fix_missing_columns',
            status: 'preview',
            details: `Would add missing column(s): ${missingCols.join(', ')}`,
        };
    }
    // Columns should already be added by ensureRequiredColumns() in getDb(),
    // but apply again defensively.
    for (const col of missingCols) {
        db.run(sql.raw(`ALTER TABLE tasks ADD COLUMN ${col} text`));
    }
    return {
        action: 'fix_missing_columns',
        status: 'applied',
        details: `Added missing column(s): ${missingCols.join(', ')}`,
    };
}
/**
 * Run all repair functions.
 * Returns all actions taken (or previewed in dry-run mode).
 */
export async function runAllRepairs(cwd, dryRun) {
    const [sizes, completedAt, columns] = await Promise.all([
        repairMissingSizes(cwd, dryRun),
        repairMissingCompletedAt(cwd, dryRun),
        repairMissingColumns(cwd, dryRun),
    ]);
    return [sizes, completedAt, columns];
}
//# sourceMappingURL=repair.js.map