import { describe, expect, it } from 'vitest'
import { adminParticipantFor } from '@/lib/domain/participants'
import { canInvite, canUploadDocuments } from '@/lib/domain/permissions'
import { canManageStatus } from '@/lib/domain/status'

/**
 * An admin who is not a participant still gets every trip tool (Nash,
 * 2026-09-17), on the page as well as in the API (2026-09-18: the Docs
 * Upload form and Order permits were hidden behind a null role while the
 * API already let admins through). One builder serves both.
 */
describe('the synthetic admin participant', () => {
  const p = adminParticipantFor('trip-1', { id: 'u-admin', email: 'admin@example.com', name: 'Nash' })

  it('carries role admin, is active, and belongs to the trip and the admin', () => {
    expect(p).toMatchObject({ trip_id: 'trip-1', user_id: 'u-admin', email: 'admin@example.com', name: 'Nash', role: 'admin', status: 'active' })
  })
  it('has no row id — nothing may be written to it', () => {
    expect(p.id).toBe('')
  })
  it('passes every per-trip gate the tools check', () => {
    expect(canUploadDocuments(p.role)).toBe(true)
    expect(canManageStatus(p.role)).toBe(true)
    for (const role of ['broker', 'dispatcher', 'driver', 'pilot', 'shipper'] as const) expect(canInvite(p.role, role)).toBe(true)
  })
  it('falls back to the email as a name', () => {
    expect(adminParticipantFor('t', { id: 'u', email: 'a@b.c' }).name).toBe('a@b.c')
  })
})
