import { integer, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'

export const inventoryItem = pgTable('inventory_item', {
  skuId: uuid('sku_id').primaryKey(),
  vendorId: text('vendor_id'),
  quantityTotal: integer('quantity_total'),
  quantitySold: integer('quantity_sold').notNull().default(0),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
  updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
})

export const stockReservation = pgTable('stock_reservation', {
  id: uuid('id').primaryKey().defaultRandom(),
  skuId: uuid('sku_id').notNull(),
  qty: integer('qty').notNull(),
  holderRef: text('holder_ref').notNull(),
  expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
  consumedAt: timestamp('consumed_at', { withTimezone: true }),
  releasedAt: timestamp('released_at', { withTimezone: true }),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export const inventorySchema = {
  inventoryItem,
  stockReservation,
}

export type InventorySchema = typeof inventorySchema
