import { describe, expect, it } from 'vitest'
import { EntitiesDomainSchema, EntityLinkSchema, EntitySchema } from '../../domains/entities'

describe('entities domain', () => {
  it('defaults to an empty entity catalog (tenant catalogs live in @repo/elevasis-core)', () => {
    expect(EntitiesDomainSchema.parse(undefined)).toEqual({})
    expect(EntitiesDomainSchema.parse({})).toEqual({})
  })

  it('enforces map key equals entity id', () => {
    expect(() =>
      EntitiesDomainSchema.parse({
        'crm.deal': {
          id: 'crm.contact',
          order: 10,
          label: 'Deal',
          ownedBySystemId: 'sales.crm'
        }
      })
    ).toThrow(/Each entity entry id must match its map key/)
  })

  it('accepts declared entity links', () => {
    expect(() => EntityLinkSchema.parse({ toEntity: 'crm.contact', kind: 'has-many' })).not.toThrow()
    expect(() =>
      EntitySchema.parse({
        id: 'crm.deal',
        order: 10,
        label: 'Deal',
        ownedBySystemId: 'sales.crm',
        links: [{ toEntity: 'crm.contact', kind: 'belongs-to', via: 'contact_id', label: 'contact' }]
      })
    ).not.toThrow()
  })
})
