import { and, eq } from 'drizzle-orm'
import { db } from '../db/db_index'
import { uploadUrlStatus } from '../db/schema'

export async function createUploadUrlStatus(key: string): Promise<void> {
  await db.insert(uploadUrlStatus).values({
    key,
    status: 'pending',
  })
}

export async function claimUploadUrlStatus(key: string): Promise<boolean> {
  const result = await db
    .update(uploadUrlStatus)
    .set({ status: 'claimed' })
    .where(and(eq(uploadUrlStatus.key, key), eq(uploadUrlStatus.status, 'pending')))
    .returning()

  return result.length > 0
}
