import { describe, expect, it } from 'vitest'
import {
  MODULE_CARD_ORDER,
  MODULE_BY_ID,
  canEnable,
  getCascadeDisables,
  getSoftImpacts,
} from '@zync/modules'

describe('module manifest', () => {
  it('keeps the spec card order and excludes the synthetic system module', () => {
    expect(MODULE_CARD_ORDER).toEqual([
      'tasks',
      'projects',
      'time_management',
      'calendar',
      'customers',
      'crm',
      'marketing',
      'invoices',
      'billing',
      'expenses',
      'contractor_payouts',
      'reports',
      'kb',
      'ai_assistant',
    ])
    expect(MODULE_CARD_ORDER).not.toContain('system')
  })

  it('models ai_assistant as a soft dependent of every spec-listed module', () => {
    expect(MODULE_BY_ID.ai_assistant.dependencies.map((dep) => dep.moduleId)).toEqual([
      'tasks',
      'customers',
      'invoices',
      'time_management',
      'calendar',
      'expenses',
      'projects',
    ])
    expect(MODULE_BY_ID.ai_assistant.dependencies.every((dep) => dep.kind === 'soft')).toBe(true)
  })
})

describe('module dependency helpers', () => {
  const enabledModules = [
    'system',
    'tasks',
    'projects',
    'time_management',
    'calendar',
    'customers',
    'crm',
    'marketing',
    'invoices',
    'billing',
    'expenses',
    'contractor_payouts',
    'reports',
    'kb',
    'ai_assistant',
  ] as const

  it('computes transitive hard cascades depth-first', () => {
    expect(getCascadeDisables('tasks', [...enabledModules])).toEqual([
      'time_management',
      'contractor_payouts',
    ])
    expect(getCascadeDisables('invoices', [...enabledModules])).toEqual(['billing'])
    expect(getCascadeDisables('time_management', [...enabledModules])).toEqual([
      'contractor_payouts',
    ])
  })

  it('collects soft impacts across the target and all hard-cascade disables', () => {
    expect(getSoftImpacts('tasks', [...enabledModules])).toEqual([
      {
        moduleId: 'projects',
        impactDescription: 'Subtask panel will be hidden in project detail.',
      },
      {
        moduleId: 'ai_assistant',
        impactDescription: 'AI features for Tasks context will be unavailable.',
      },
      {
        moduleId: 'reports',
        impactDescription: 'Utilization and billable-hours reports unavailable.',
      },
    ])
  })

  it('never reports a soft-impact module that is being hard-disabled in the same action', () => {
    const impacts = getSoftImpacts('invoices', [...enabledModules])
    expect(impacts.some((impact) => impact.moduleId === 'billing')).toBe(false)
  })

  it('blocks enabling modules whose hard dependencies are still disabled', () => {
    expect(canEnable('billing', ['system', 'customers'])).toEqual({
      allowed: false,
      missingHardDeps: ['invoices'],
    })
    expect(canEnable('contractor_payouts', ['system', 'tasks'])).toEqual({
      allowed: false,
      missingHardDeps: ['time_management'],
    })
    expect(canEnable('projects', ['system'])).toEqual({
      allowed: true,
      missingHardDeps: [],
    })
  })
})
