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

export const companies = pgTable('company', {
  id: uuid('id').primaryKey().defaultRandom(),
  name: varchar('name', { length: 255 }).notNull(),
  phone: varchar('phone', { length: 50 }),
  email: varchar('email', { length: 255 }),
  address: jsonb('address').$type<{
    street?: string
    city?: string
    state?: string
    zip?: string
  }>(),
  timezone: varchar('timezone', { length: 100 }).notNull().default('America/Chicago'),
  logoFileId: uuid('logo_file_id'),
  notes: text('notes'),
  createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
  updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
})

export const locations = pgTable('location', {
  id: uuid('id').primaryKey().defaultRandom(),
  name: varchar('name', { length: 255 }).notNull(),
  address: jsonb('address').$type<{
    street?: string
    city?: string
    state?: string
    zip?: string
  }>(),
  phone: varchar('phone', { length: 50 }),
  email: varchar('email', { length: 255 }),
  timezone: varchar('timezone', { length: 100 }),
  taxRate: numeric('tax_rate', { precision: 5, scale: 4 }),
  serviceTaxRate: numeric('service_tax_rate', { precision: 5, scale: 4 }),
  cashRounding: boolean('cash_rounding').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(),
})

export const appConfig = pgTable('app_config', {
  key: varchar('key', { length: 100 }).primaryKey(),
  value: text('value'),
  description: text('description'),
  updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
})

export type Company = typeof companies.$inferSelect
export type CompanyInsert = typeof companies.$inferInsert
export type Location = typeof locations.$inferSelect
export type LocationInsert = typeof locations.$inferInsert
export type AppConfig = typeof appConfig.$inferSelect
export type AppConfigInsert = typeof appConfig.$inferInsert
