/**
 * Deal lifecycle — single-entity expiry logic.
 *
 * Called by both DealDO.alarm() and can be used by the deal-expiry cron handler.
 *
 * Deps shape: { db } — no cloudflare:workers imports; importable by the DO worker.
 */

import type { DoDbClient } from '../lib/db.js';
import { expireDealRows, clearExpiredGoldWindow } from '@/server/db/queries/do-lifecycle.js';

export interface DealExpireDeps {
  db: DoDbClient;
}

/**
 * Expire a single deal by id:
 *  1. Transition ACTIVE/PAUSED deals with windowEnd < now to EXPIRED.
 *  2. Expire all UNREDEEMED vouchers for this deal whose expiresAt < now.
 *
 * Idempotent: WHERE guards prevent double-transitions.
 */
export async function expireDeal(deps: DealExpireDeps, dealId: string): Promise<void> {
  await expireDealRows(deps.db, dealId, new Date());
}

/**
 * Expire the gold shimmer window for a single deal by id.
 * Clears soldOutGoldExpiresAt when window has passed.
 */
export async function expireGoldWindow(deps: DealExpireDeps, dealId: string): Promise<void> {
  await clearExpiredGoldWindow(deps.db, dealId, new Date());
}
