import { describe, expect, it } from 'vitest'
import * as helpdesk from './index.js'
import { listMessages, postMessage } from './messages.js'
import { createTestDb } from './test-fixture.js'

describe('postMessage / listMessages', () => {
  it('does not return a message posted under (case, id) when reading (ticket, same id)', async () => {
    const db = await createTestDb()

    await postMessage(db, { parentType: 'case', parentId: '1' }, {
      authorType: 'staff',
      body: 'case-only thread',
    })

    const ticketRows = await listMessages(db, { parentType: 'ticket', parentId: '1' })
    expect(ticketRows).toEqual([])
  })

  it('returns only rows scoped to the requested (parentType, parentId)', async () => {
    const db = await createTestDb()

    await postMessage(db, { parentType: 'ticket', parentId: '42' }, {
      authorType: 'customer',
      body: 'ticket thread',
    })
    await postMessage(db, { parentType: 'case', parentId: '42' }, {
      authorType: 'system',
      body: 'case thread',
    })

    const ticketRows = await listMessages(db, { parentType: 'ticket', parentId: '42' })
    expect(ticketRows).toHaveLength(1)
    expect(ticketRows[0]?.body).toBe('ticket thread')
  })

  it('exports no transition mutating helpers from the transitions seam', () => {
    expect(helpdesk).not.toHaveProperty('updateTransition')
    expect(helpdesk).not.toHaveProperty('deleteTransition')
  })
})
