import { getOrderById, order } from "@platform-modules/commerce-orders";
import { and, asc, eq, lt } from "drizzle-orm";
//#region src/reconcile/index.ts
const DEFAULT_OLDER_THAN_MS = 9e5;
const DEFAULT_LIMIT = 100;
async function reconcileStuckCharges(deps, opts) {
	const olderThanMs = opts?.olderThanMs ?? DEFAULT_OLDER_THAN_MS;
	const cutoff = new Date(Date.now() - olderThanMs);
	const rows = await deps.db.select({ id: order.id }).from(order).where(and(eq(order.status, "charging"), lt(order.updatedAt, cutoff))).orderBy(asc(order.updatedAt)).limit(opts?.limit ?? DEFAULT_LIMIT);
	const stuck = [];
	for (const row of rows) {
		const loaded = await getOrderById(deps.db, row.id, { isAdmin: true });
		if (loaded) stuck.push(loaded);
	}
	return { stuck };
}
//#endregion
export { reconcileStuckCharges };
