import { describe, expect, it } from 'vitest'
import { DEFAULT_ORGANIZATION_MODEL_BRANDING } from '../domains/branding'
import { OrganizationModelSchema } from '../schema'
import { resolveOrganizationModel } from '../resolve'

function minimalModel(systems: Record<string, unknown>) {
  return {
    version: 1,
    branding: DEFAULT_ORGANIZATION_MODEL_BRANDING,
    entities: {},
    systems
  }
}

describe('recursive System schema after System.content retirement', () => {
  it('parses recursively authored systems with canonical systems children', () => {
    const model = resolveOrganizationModel(
      minimalModel({
        parent: {
          id: 'parent',
          order: 10,
          label: 'Parent',
          systems: {
            child: {
              id: 'parent.child',
              order: 20,
              label: 'Child'
            }
          }
        }
      })
    )

    expect(model.systems.parent?.systems?.child?.id).toBe('parent.child')
    expect(model.systems.parent?.subsystems?.child?.id).toBe('parent.child')
  })

  it('keeps subsystems as a compatibility alias for recursive traversal', () => {
    const model = resolveOrganizationModel(
      minimalModel({
        parent: {
          id: 'parent',
          order: 10,
          label: 'Parent',
          subsystems: {
            child: {
              id: 'parent.child',
              order: 20,
              label: 'Child'
            }
          }
        }
      })
    )

    expect(model.systems.parent?.subsystems?.child?.id).toBe('parent.child')
  })

  it('rejects content maps on top-level systems', () => {
    const result = OrganizationModelSchema.safeParse(
      minimalModel({
        dashboard: {
          id: 'dashboard',
          order: 10,
          label: 'Dashboard',
          content: {
            pipeline: {
              kind: 'schema',
              type: 'pipeline',
              label: 'Pipeline',
              data: { entityId: 'crm.deal' }
            }
          }
        }
      })
    )

    expect(result.success).toBe(false)
    const contentIssue = result.error?.issues.find((issue) => issue.path.join('.') === 'systems.dashboard')
    expect(contentIssue).toMatchObject({
      code: 'unrecognized_keys',
      path: ['systems', 'dashboard']
    })
  })

  it('rejects content maps on nested systems', () => {
    const result = OrganizationModelSchema.safeParse(
      minimalModel({
        parent: {
          id: 'parent',
          order: 10,
          label: 'Parent',
          systems: {
            child: {
              id: 'parent.child',
              order: 20,
              label: 'Child',
              content: {
                settings: {
                  kind: 'config',
                  type: 'kv',
                  label: 'Settings',
                  data: { entries: { mode: 'bridge' } }
                }
              }
            }
          }
        }
      })
    )

    expect(result.success).toBe(false)
    const contentIssue = result.error?.issues.find((issue) => issue.path.join('.') === 'systems.parent.systems.child')
    expect(contentIssue).toMatchObject({
      code: 'unrecognized_keys',
      path: ['systems', 'parent', 'systems', 'child']
    })
  })

  it('accepts equivalent catalog authoring in system ontology', () => {
    const result = OrganizationModelSchema.safeParse(
      minimalModel({
        dashboard: {
          id: 'dashboard',
          order: 10,
          label: 'Dashboard',
          ontology: {
            objectTypes: {
              'dashboard:object/deal': {
                id: 'dashboard:object/deal',
                label: 'Deal',
                ownerSystemId: 'dashboard'
              }
            },
            catalogTypes: {
              'dashboard:catalog/pipeline': {
                id: 'dashboard:catalog/pipeline',
                label: 'Pipeline',
                kind: 'pipeline',
                appliesTo: 'dashboard:object/deal',
                entries: {
                  open: { label: 'Open', order: 10, semanticClass: 'open' }
                }
              }
            }
          }
        }
      })
    )

    expect(result.success).toBe(true)
  })

  it('accepts equivalent local settings in first-class system config', () => {
    const model = resolveOrganizationModel(
      minimalModel({
        dashboard: {
          id: 'dashboard',
          order: 10,
          label: 'Dashboard',
          config: {
            mode: 'direct',
            retries: 3,
            nested: { enabled: true }
          }
        }
      })
    )

    expect(model.systems.dashboard?.config).toEqual({
      mode: 'direct',
      retries: 3,
      nested: { enabled: true }
    })
  })
})
