import { describe, expect, it } from 'vitest'
import { createFoundationOrganizationModel } from '../foundation'

const foundationFixture = {
  branding: { organizationName: 'Acme', productName: 'Acme OS', shortName: 'Acme' },
  systems: {
    dashboard: { id: 'dashboard', order: 10, label: 'Dashboard', lifecycle: 'active' as const },
    sales: { id: 'sales', order: 20, label: 'Sales', lifecycle: 'active' as const },
    clients: { id: 'clients', order: 30, label: 'Clients', lifecycle: 'active' as const },
    projects: { id: 'projects', order: 40, label: 'Projects', lifecycle: 'active' as const },
    operations: { id: 'operations', order: 50, label: 'Operations', lifecycle: 'active' as const },
    'settings.account': { id: 'settings.account', order: 60, label: 'Account', lifecycle: 'active' as const }
  },
  navigation: {
    sidebar: {
      primary: {
        dashboard: {
          type: 'surface' as const,
          label: 'Dashboard',
          path: '/',
          surfaceType: 'dashboard' as const,
          order: 10,
          targets: { systems: ['dashboard'] }
        },
        sales: {
          type: 'surface' as const,
          label: 'Sales',
          path: '/sales',
          surfaceType: 'page' as const,
          order: 20,
          targets: { systems: ['sales'] }
        },
        clients: {
          type: 'surface' as const,
          label: 'Clients',
          path: '/clients',
          surfaceType: 'list' as const,
          order: 30,
          targets: { systems: ['clients'] }
        },
        projects: {
          type: 'surface' as const,
          label: 'Projects',
          path: '/projects',
          surfaceType: 'page' as const,
          order: 40,
          targets: { systems: ['projects'] }
        },
        'operations-overview': {
          type: 'surface' as const,
          label: 'Operations',
          path: '/operations',
          surfaceType: 'page' as const,
          order: 50,
          targets: { systems: ['operations'] }
        }
      },
      bottom: {
        'settings-account': {
          type: 'surface' as const,
          label: 'Account',
          path: '/settings/account',
          surfaceType: 'settings' as const,
          order: 10,
          targets: { systems: ['settings.account'] }
        }
      }
    }
  }
}

describe('createFoundationOrganizationModel', () => {
  it('builds the foundation model from derived sidebar navigation', () => {
    const result = createFoundationOrganizationModel(foundationFixture)

    expect(result.canonical.branding.organizationName).toBe('Acme')
    expect(result.homeLabel).toBe('Dashboard')
    expect(result.model.navigation.defaultSurfaceId).toBe('dashboard')

    const surfaces = result.model.navigation.surfaces
    expect(surfaces.find((s) => s.id === 'dashboard')).toMatchObject({ path: '/', surfaceType: 'dashboard' })
    expect(surfaces.find((s) => s.id === 'sales')).toBeDefined()
    expect(surfaces.find((s) => s.id === 'clients')).toMatchObject({ path: '/clients' })
    expect(surfaces.find((s) => s.id === 'projects')).toBeDefined()
    expect(surfaces.find((s) => s.id === 'operations-overview')).toBeDefined()
    expect(surfaces.find((s) => s.id === 'settings-account')).toBeDefined()

    expect(result.quickAccessSurfaceIds).toContain('sales')
    expect(result.quickAccessSurfaceIds).toContain('clients')
    expect(result.quickAccessSurfaceIds).toContain('projects')
    expect(result.quickAccessSurfaceIds).toContain('operations-overview')
  })

  it.skip('passes deeper overrides through to the canonical model (deferred - Phase 4: sales domain removed)', () => {
    // Previously tested: result.canonical.sales.pipelines.
    // Re-enable with an ontology catalog based pipeline override test if needed.
  })

  it('exposes a working getOrganizationSurface lookup', () => {
    const result = createFoundationOrganizationModel(foundationFixture)

    const clientsSurface = result.getOrganizationSurface('clients')
    expect(clientsSurface).toBeDefined()
    expect(clientsSurface?.id).toBe('clients')
    expect(clientsSurface?.path).toBe('/clients')

    expect(result.getOrganizationSurface('nonexistent')).toBeUndefined()
  })

  it('does not require hardcoded system-backed foundation surfaces', () => {
    const result = createFoundationOrganizationModel({
      systems: {
        dashboard: { id: 'dashboard', order: 10, label: 'Dashboard', lifecycle: 'active' }
      },
      navigation: {
        sidebar: {
          primary: {
            dashboard: {
              type: 'surface',
              label: 'Home',
              path: '/',
              surfaceType: 'dashboard',
              order: 10,
              targets: { systems: ['dashboard'] }
            },
            custom: {
              type: 'surface',
              label: 'Custom',
              path: '/custom',
              surfaceType: 'page',
              order: 20,
              targets: { systems: [] }
            }
          },
          bottom: {}
        }
      }
    })

    expect(result.model.navigation.defaultSurfaceId).toBe('dashboard')
    expect(result.homeLabel).toBe('Home')
    expect(result.getOrganizationSurface('custom')).toMatchObject({ label: 'Custom', path: '/custom' })
  })
})
