import { describe, expect, it } from 'vitest'
import { lineMatchesScope } from './scope.js'
import type { DiscountLine } from './types.js'

const baseLine = (overrides: Partial<DiscountLine> = {}): DiscountLine => ({
  lineId: 'line-1',
  unitPrice: 1_000n,
  qty: 1,
  vendorId: null,
  ...overrides,
})

describe('lineMatchesScope', () => {
  it('matches all scope for any line', () => {
    expect(lineMatchesScope(baseLine(), { kind: 'all' })).toBe(true)
  })

  it('matches products scope when productId is listed', () => {
    expect(
      lineMatchesScope(baseLine({ productId: 'prod-1' }), {
        kind: 'products',
        ids: ['prod-1', 'prod-2'],
      }),
    ).toBe(true)
  })

  it('matches categories scope when categoryIds intersect', () => {
    expect(
      lineMatchesScope(baseLine({ categoryIds: ['cat-a', 'cat-b'] }), {
        kind: 'categories',
        ids: ['cat-b'],
      }),
    ).toBe(true)
  })

  it('matches tags scope when tags intersect', () => {
    expect(
      lineMatchesScope(baseLine({ tags: ['sale', 'new'] }), {
        kind: 'tags',
        tags: ['new'],
      }),
    ).toBe(true)
  })

  it('matches vendor scope when vendorId equals', () => {
    expect(
      lineMatchesScope(baseLine({ vendorId: 'vendor-1' }), {
        kind: 'vendor',
        vendorId: 'vendor-1',
      }),
    ).toBe(true)
  })

  it('fail-closed: products scope does not match line missing productId', () => {
    expect(
      lineMatchesScope(baseLine(), {
        kind: 'products',
        ids: ['prod-1'],
      }),
    ).toBe(false)
  })
})
