import { eq } from 'drizzle-orm'
import { describe, expect, it } from 'vitest'
import { createPgliteClient } from '../../db/src/postgres/pglite.js'
import { pushSchema } from './migrate.js'
import { catalogSchema, product, variant, variantPrice } from './schema.js'

describe('commerce-catalog schema', () => {
  it('round-trips product, variant, and variant_price with bigint amount', async () => {
    const db = createPgliteClient({ schema: catalogSchema })
    await pushSchema(db)

    const [p] = await db
      .insert(product)
      .values({ kind: 'physical', slug: 'widget', title: 'Widget' })
      .returning()

    const [v] = await db
      .insert(variant)
      .values({ productId: p!.id, sku: 'WIDGET-1' })
      .returning()

    await db.insert(variantPrice).values({
      variantId: v!.id,
      currency: 'USD',
      amount: 1999n,
      priceMode: 'exclusive',
    })

    const [priceRow] = await db
      .select()
      .from(variantPrice)
      .where(eq(variantPrice.variantId, v!.id))

    expect(priceRow?.amount).toBe(1999n)
    expect(typeof priceRow?.amount).toBe('bigint')
    expect(priceRow?.priceMode).toBe('exclusive')
    expect(priceRow?.currency).toBe('USD')
  })
})
