import type { DrizzleClient, TxDrizzleClient } from '@/server/db/client';
import { createMaturityHostStore } from '@/server/affiliate-module/maturity-host-store.js';
import { sweepWithdrawableDriver } from '@/server/affiliate-module/sweep-driver.js';

/**
 * Withdrawable sweep: anchor withdrawable_at then recompute wallet_vesting.withdrawable_minor.
 *
 * Live path — delegates to sweepWithdrawableDriver + MaturityHostStore injection
 * (module sweepWithdrawable + per-owner recomputeWithdrawable inside the driver tx).
 */
export async function runWithdrawableSweep(
  db: DrizzleClient,
  disputeWindowDays: number,
): Promise<{ updated: number }> {
  const dw = Number(disputeWindowDays);
  if (!Number.isInteger(dw) || dw < 0 || dw > 3650) {
    return { updated: 0 };
  }
  return sweepWithdrawableDriver(db as TxDrizzleClient, createMaturityHostStore(db));
}
