/**
 * Unit tests for getResourcesForSystem helper.
 *
 * Coverage:
 * - Empty model (no resources)
 * - Single leaf resource, exact match
 * - includeDescendants: true returns descendants
 * - Deep nesting (three levels)
 * - Segment-boundary safety: 'sales' must NOT match 'salesforce.foo'
 * - Root path with includeDescendants: false
 * - Root path with includeDescendants: true
 * - Mixed resources, only matching ones returned
 */
import { describe, it, expect } from 'vitest'
import { getResourcesForSystem } from '../helpers'
import type { OrganizationModel } from '../types'
import type { ResourceEntry } from '../domains/resources'

// ---------------------------------------------------------------------------
// Minimal fixture builder
// ---------------------------------------------------------------------------

function makeModel(resources: Record<string, ResourceEntry>): OrganizationModel {
  return {
    systems: {},
    resources,
    roles: {},
    goals: {},
    knowledge: {},
    actions: {},
    entities: {},
    policies: {},
    navigation: { sidebar: { primary: {}, bottom: {} } }
  } as unknown as OrganizationModel
}

function makeWorkflow(id: string, systemPath: string): ResourceEntry {
  return {
    id,
    order: 10,
    kind: 'workflow',
    systemPath,
    status: 'active'
  }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

describe('getResourcesForSystem', () => {
  it('returns empty array when model has no resources', () => {
    const model = makeModel({})
    expect(getResourcesForSystem(model, 'sales')).toEqual([])
  })

  it('returns empty array when no resources match the path', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'marketing')
    })
    expect(getResourcesForSystem(model, 'sales')).toEqual([])
  })

  it('returns a single resource with exact match', () => {
    const r = makeWorkflow('wf-a', 'sales')
    const model = makeModel({ 'wf-a': r })
    const result = getResourcesForSystem(model, 'sales')
    expect(result).toHaveLength(1)
    expect(result[0].id).toBe('wf-a')
  })

  it('does NOT return descendants when includeDescendants is false (default)', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'sales'),
      'wf-b': makeWorkflow('wf-b', 'sales.crm'),
      'wf-c': makeWorkflow('wf-c', 'sales.lead-gen')
    })
    const result = getResourcesForSystem(model, 'sales')
    expect(result).toHaveLength(1)
    expect(result[0].id).toBe('wf-a')
  })

  it('returns exact match AND descendants when includeDescendants is true', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'sales'),
      'wf-b': makeWorkflow('wf-b', 'sales.crm'),
      'wf-c': makeWorkflow('wf-c', 'sales.lead-gen'),
      'wf-d': makeWorkflow('wf-d', 'marketing')
    })
    const result = getResourcesForSystem(model, 'sales', { includeDescendants: true })
    const ids = result.map((r) => r.id).sort()
    expect(ids).toEqual(['wf-a', 'wf-b', 'wf-c'])
  })

  it('matches three-level deep descendants', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'sales.crm.dispositions'),
      'wf-b': makeWorkflow('wf-b', 'sales.crm'),
      'wf-c': makeWorkflow('wf-c', 'sales')
    })
    const result = getResourcesForSystem(model, 'sales', { includeDescendants: true })
    expect(result).toHaveLength(3)
  })

  it('segment-boundary: "sales" does NOT match "salesforce.foo"', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'salesforce.foo'),
      'wf-b': makeWorkflow('wf-b', 'salesforce'),
      'wf-c': makeWorkflow('wf-c', 'sales')
    })
    const result = getResourcesForSystem(model, 'sales', { includeDescendants: true })
    // Only exact 'sales' match — 'salesforce' and 'salesforce.foo' must NOT appear
    expect(result).toHaveLength(1)
    expect(result[0].id).toBe('wf-c')
  })

  it('segment-boundary: "sys" does NOT match "sys2.foo" or "system.bar"', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'sys'),
      'wf-b': makeWorkflow('wf-b', 'sys.platform'),
      'wf-c': makeWorkflow('wf-c', 'sys2.foo'),
      'wf-d': makeWorkflow('wf-d', 'system.bar')
    })
    const result = getResourcesForSystem(model, 'sys', { includeDescendants: true })
    const ids = result.map((r) => r.id).sort()
    expect(ids).toEqual(['wf-a', 'wf-b'])
  })

  it('root path with includeDescendants false returns only exact root resources', () => {
    const model = makeModel({
      'wf-a': makeWorkflow('wf-a', 'sys'),
      'wf-b': makeWorkflow('wf-b', 'sys.platform'),
      'wf-c': makeWorkflow('wf-c', 'sys.diagnostic')
    })
    const result = getResourcesForSystem(model, 'sys')
    expect(result).toHaveLength(1)
    expect(result[0].id).toBe('wf-a')
  })

  it('handles undefined resources gracefully via nullish coalescing', () => {
    const model = { systems: {}, resources: undefined } as unknown as OrganizationModel
    expect(getResourcesForSystem(model, 'sales')).toEqual([])
  })
})
