import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import type { TransactionalDatabase } from '@platform-modules/db'
import { createShipment } from './shipment.js'
import { startPg } from '../pg-harness.js'
import type { FulfillmentSchema } from '../schema.js'
import type { Address } from '../types.js'

const ORDER_ID = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'

const ADDRESS: Address = {
  line1: '42 Ship St',
  city: 'Parcelton',
  postalCode: '90210',
  country: 'US',
}

describe('createShipment', () => {
  let db: TransactionalDatabase<FulfillmentSchema>
  let stop: (() => Promise<void>) | undefined

  beforeAll(async () => {
    const pgResult = await startPg()
    db = pgResult.db
    stop = pgResult.stop
  }, 120_000)

  afterAll(async () => {
    await stop?.()
  }, 30_000)

  it('inserts a shipment row with status pending', async () => {
    const shipment = await db.transaction((tx) =>
      createShipment(tx, {
        orderId: ORDER_ID,
        lineIds: ['line-physical-1'],
        address: ADDRESS,
      }),
    )

    expect(shipment.orderId).toBe(ORDER_ID)
    expect(shipment.lineIds).toEqual(['line-physical-1'])
    expect(shipment.address).toEqual(ADDRESS)
    expect(shipment.status).toBe('pending')
    expect(shipment.labelId).toBeNull()
    expect(shipment.trackingNumber).toBeNull()
  })
})
