import {
  pgTable,
  text,
  integer,
  timestamp,
  jsonb,
  boolean
} from 'drizzle-orm/pg-core'
import { products } from './products'
import { relations } from 'drizzle-orm'

export const prices = pgTable('prices', {
  id: text('id').primaryKey().notNull(), // Stripe Price ID
  description: text('description'),
  currency: text('currency').notNull(),
  unitAmount: integer('unit_amount').notNull(),
  type: text('type').notNull(),
  interval: text('interval').notNull(),
  intervalCount: integer('interval_count').notNull(),
  trialPeriodDays: integer('trial_period_days'),
  active: boolean('active').notNull().default(true),
  metadata: jsonb('metadata'),
  productId: text('product_id')
    .notNull()
    .references(() => products.id, { onDelete: 'cascade' }),
  createdAt: timestamp('created_at').defaultNow(),
  updatedAt: timestamp('updated_at').defaultNow()
})

export const pricesRelations = relations(prices, ({ one }) => ({
  product: one(products, {
    fields: [prices.productId],
    references: [products.id]
  })
}))
