import { boolean, index, pgTable, text, timestamp, uniqueIndex, uuid } from 'drizzle-orm/pg-core'
import { tenants } from './tenants'

export const stockLocations = pgTable(
  'stock_locations',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    tenantId: uuid('tenant_id')
      .notNull()
      .references(() => tenants.id, { onDelete: 'cascade' }),
    name: text('name').notNull(),
    code: text('code'),
    isDefault: boolean('is_default').notNull().default(false),
    isActive: boolean('is_active').notNull().default(true),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
    updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => ({
    tenantIdx: index('idx_stock_locations_tenant').on(t.tenantId),
    codeUnique: uniqueIndex('uq_stock_locations_tenant_code').on(t.tenantId, t.code),
  }),
)

export type StockLocationRow = typeof stockLocations.$inferSelect
export type NewStockLocationRow = typeof stockLocations.$inferInsert
