// adiuvAI/src/main/files/daily-rescan.ts
import { getDb } from '../db';
import { projects } from '../db/schema';
import { sql, and, isNotNull } from 'drizzle-orm';
import { startIndexSession } from './indexer';

const ONE_DAY_MS = 24 * 60 * 60 * 1000;

export async function runDailyRescan(): Promise<void> {
  const cutoff = Date.now() - ONE_DAY_MS;
  const stale = getDb()
    .select()
    .from(projects)
    .where(
      and(
        isNotNull(projects.folderPath),
        sql`(${projects.folderLastScannedAt} IS NULL OR ${projects.folderLastScannedAt} < ${cutoff})`,
      ),
    )
    .all();
  for (const p of stale) {
    if (p.folderLastScanStatus === 'scanning') continue;
    // Fire-and-forget; no UI listener.
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    void startIndexSession(p.id, () => {});
  }
}
