import { integer, pgTable, text, timestamp, uniqueIndex, uuid } from 'drizzle-orm/pg-core'
import { catalogSchema } from '@platform-modules/commerce-catalog'
import { ordersSchema } from '@platform-modules/commerce-orders'
import type { VendorStatus } from './types.js'

export const vendor = pgTable(
  'vendor',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    ownerUserId: text('owner_user_id').notNull(),
    name: text('name').notNull(),
    status: text('status').$type<VendorStatus>().notNull().default('pending'),
    commissionBps: integer('commission_bps').notNull(),
    createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
    updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
  },
  (t) => [uniqueIndex('vendor_owner_user_id_uq').on(t.ownerUserId)],
)

export const marketplaceSchema = {
  vendor,
}

export const marketplaceDbSchema = {
  ...marketplaceSchema,
  ...catalogSchema,
  ...ordersSchema,
}

export type MarketplaceSchema = typeof marketplaceSchema
export type MarketplaceDbSchema = typeof marketplaceDbSchema
