import { and, eq, sql } from 'drizzle-orm';
import type { DrizzleClient } from '../../client';
import { deals, vendors } from '../../schema';
export async function approve(db: DrizzleClient, id: string) {
  return db
    .update(deals)
    .set({ dealState: 'ACTIVE', approvedAt: new Date(), approvedBy: 'AI_AGENT' })
    .where(and(eq(deals.id, id), eq(deals.dealState, 'UNDER_REVIEW')))
    .returning({ id: deals.id });
}
export async function flag(db: DrizzleClient, id: string) {
  return db
    .update(deals)
    .set({ dealState: 'PENDING_APPROVAL' })
    .where(and(eq(deals.id, id), eq(deals.dealState, 'UNDER_REVIEW')))
    .returning({ id: deals.id, vendorId: deals.vendorId });
}
export async function incrementVendorViolations(db: DrizzleClient, id: string) {
  await db
    .update(vendors)
    .set({ dealViolationsCount: sql`${vendors.dealViolationsCount} + 1` })
    .where(eq(vendors.id, id));
}
export async function escalate(db: DrizzleClient, id: string) {
  return db
    .update(deals)
    .set({ dealState: 'PENDING_APPROVAL' })
    .where(and(eq(deals.id, id), eq(deals.dealState, 'UNDER_REVIEW')))
    .returning({ id: deals.id, vendorId: deals.vendorId });
}
