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

/**
 * Lookup tables replace hard-coded pgEnums for values that stores may want to customize.
 * System rows (is_system = true) are seeded per company and cannot be deleted or renamed.
 * Stores can add custom rows for informational/tracking purposes.
 */

export const inventoryUnitStatuses = pgTable('inventory_unit_status', {
  id: uuid('id').primaryKey().defaultRandom(),
  name: varchar('name', { length: 100 }).notNull(),
  slug: varchar('slug', { length: 100 }).notNull(),
  description: text('description'),
  isSystem: boolean('is_system').notNull().default(false),
  sortOrder: integer('sort_order').notNull().default(0),
  isActive: boolean('is_active').notNull().default(true),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export const itemConditions = pgTable('item_condition', {
  id: uuid('id').primaryKey().defaultRandom(),
  name: varchar('name', { length: 100 }).notNull(),
  slug: varchar('slug', { length: 100 }).notNull(),
  description: text('description'),
  isSystem: boolean('is_system').notNull().default(false),
  sortOrder: integer('sort_order').notNull().default(0),
  isActive: boolean('is_active').notNull().default(true),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})

export type InventoryUnitStatus = typeof inventoryUnitStatuses.$inferSelect
export type InventoryUnitStatusInsert = typeof inventoryUnitStatuses.$inferInsert
export type ItemCondition = typeof itemConditions.$inferSelect
export type ItemConditionInsert = typeof itemConditions.$inferInsert

/**
 * System-seeded inventory unit statuses.
 * Code references these slugs for business logic.
 */
export const SYSTEM_UNIT_STATUSES = [
  { slug: 'available', name: 'Available', description: 'In stock, ready for sale or rental', sortOrder: 0 },
  { slug: 'sold', name: 'Sold', description: 'Purchased by customer', sortOrder: 1 },
  { slug: 'rented', name: 'Rented', description: 'Out on active rental contract', sortOrder: 2 },
  { slug: 'on_trial', name: 'On Trial', description: 'Out with customer on in-home trial', sortOrder: 3 },
  { slug: 'in_repair', name: 'In Repair', description: 'In repair shop for service', sortOrder: 4 },
  { slug: 'layaway', name: 'Layaway', description: 'Reserved for layaway customer', sortOrder: 5 },
  { slug: 'lost', name: 'Lost', description: 'Unrecovered from trial, rental, or discrepancy', sortOrder: 6 },
  { slug: 'retired', name: 'Retired', description: 'Permanently removed from inventory', sortOrder: 7 },
] as const

/**
 * System-seeded item conditions.
 */
export const SYSTEM_ITEM_CONDITIONS = [
  { slug: 'new', name: 'New', description: 'Brand new, unopened or unused', sortOrder: 0 },
  { slug: 'excellent', name: 'Excellent', description: 'Like new, minimal signs of use', sortOrder: 1 },
  { slug: 'good', name: 'Good', description: 'Normal wear, fully functional', sortOrder: 2 },
  { slug: 'fair', name: 'Fair', description: 'Noticeable wear, functional with minor issues', sortOrder: 3 },
  { slug: 'poor', name: 'Poor', description: 'Heavy wear, may need repair', sortOrder: 4 },
] as const
