import { describe, expect, it, vi } from 'vitest'
import { getInventoryReport } from '../../src/queries/inventory'

describe('getInventoryReport', () => {
  it('serializes tenant-scoped balance and COGS rows', async () => {
    const execute = vi.fn().mockResolvedValue({
      rows: [
        {
          product_id: 'product-1',
          product_name: 'Paper',
          stock_item_id: 'stock-1',
          location_id: 'location-1',
          location_name: 'Main',
          location_code: 'MAIN',
          category: 'Office',
          unit: 'unit',
          quantity_on_hand: '4',
          avg_unit_cost: '12.50',
          valuation_amount: '50',
          cogs_amount: '8.25',
        },
      ],
    })

    const report = await getInventoryReport({ execute } as never, 'tenant-1', {
      asOf: '2026-07-07',
      from: '2026-07-01',
      to: '2026-07-07',
      locationId: '11111111-1111-4111-8111-111111111111',
    })

    expect(report).toEqual({
      asOf: '2026-07-07',
      from: '2026-07-01',
      to: '2026-07-07',
      summary: {
        valuationAmount: 50,
        cogsAmount: 8.25,
        itemCount: 1,
        quantityOnHand: 4,
      },
      rows: [
        expect.objectContaining({
          productId: 'product-1',
          locationId: 'location-1',
          quantityOnHand: 4,
          avgUnitCost: 12.5,
        }),
      ],
    })

    expect(JSON.stringify(execute.mock.calls[0]?.[0])).toContain('tenant-1')
  })
})
