import {
  listDueDynamicQrTransactions,
  toTransactionPayload,
  updateTransactionStatus
} from "../store/transactionStore";

export async function expireDueDynamicQrTransactions(input?: {
  limit?: number;
  source?: "system" | "admin";
  request_id?: string;
}) {
  const due = await listDueDynamicQrTransactions(input?.limit || 100);
  const expired = [];
  const skipped = [];
  const sweptAt = new Date().toISOString();

  for (const tx of due) {
    try {
      const updated = await updateTransactionStatus(tx.id, "expired", {
        source: input?.source || "system",
        expired_at: tx.expired_at || sweptAt,
        eventContext: {
          reason: "dynamic_qr_expired",
          expired_at: tx.expired_at,
          swept_at: sweptAt,
          request_id: input?.request_id
        }
      });
      expired.push(toTransactionPayload(updated));
    } catch (error) {
      skipped.push({
        transaction_id: tx.id,
        partner_reference: tx.partner_reference,
        reason: error instanceof Error ? error.message : "UNKNOWN_ERROR"
      });
    }
  }

  return {
    scanned: due.length,
    expired_count: expired.length,
    skipped_count: skipped.length,
    swept_at: sweptAt,
    expired,
    skipped
  };
}
