import { sql } from 'drizzle-orm'
import { describe, expect, it } from 'vitest'
import { createPgliteClient } from '@platform-modules/db/pglite'
import { pushSchema } from './migrate.js'
import { inventorySchema } from './schema.js'

describe('pushSchema', () => {
  it('creates all inventory foundation tables', async () => {
    const db = createPgliteClient({ schema: inventorySchema })
    await pushSchema(db)

    const result = await db.execute(sql`
      SELECT table_name
      FROM information_schema.tables
      WHERE table_schema = 'public'
        AND table_name IN (
          'stock_location',
          'stock_item',
          'stock_movement',
          'stock_cost_layer',
          'stock_position'
        )
      ORDER BY table_name
    `)

    const rows = (Array.isArray(result) ? result : result.rows) as Array<{ table_name: string }>
    expect(rows.map((row) => row.table_name)).toEqual([
      'stock_cost_layer',
      'stock_item',
      'stock_location',
      'stock_movement',
      'stock_position',
    ])
  })
})
