/** @type {import('dependency-cruiser').IConfiguration} */
module.exports = {
  forbidden: [
    // No circular dependencies
    {
      name: 'no-circular',
      severity: 'error',
      comment: 'Circular dependencies are not allowed.',
      from: {},
      to: { circular: true },
    },

    // Modules must not import internals of other modules directly.
    // Only import via the module's public barrel (index.ts).
    {
      name: 'no-cross-module-internals',
      severity: 'error',
      comment:
        'Modules must not import internal files of other modules. Import from the module index (barrel) instead.',
      from: {
        path: 'src/modules/([^/]+)/',
      },
      to: {
        path: 'src/modules/([^/]+)/.+',
        pathNot: [
          // Allow importing from the module's own files
          'src/modules/$1/',
          // Allow importing from another module's barrel index
          'src/modules/[^/]+/index\\.ts$',
        ],
      },
    },

    // Apps (outside modules) should not import module internals either.
    // Files inside src/modules/ are covered by no-cross-module-internals above.
    {
      name: 'no-app-to-module-internals',
      severity: 'error',
      comment: 'Apps must import modules via their barrel index, not internal files.',
      from: {
        path: 'apps/',
        pathNot: ['src/modules/'],
      },
      to: {
        path: 'src/modules/([^/]+)/.+',
        pathNot: ['src/modules/[^/]+/index\\.ts$'],
      },
    },

    // No orphan modules (files not reachable from any entry point)
    {
      name: 'no-orphans',
      severity: 'warn',
      comment: 'Orphan modules may indicate dead code.',
      from: {
        orphan: true,
        pathNot: ['\\.(spec|test)\\.ts$', '__tests__/', '\\.d\\.ts$', 'index\\.ts$'],
      },
      to: {},
    },
  ],

  options: {
    doNotFollow: {
      path: ['node_modules', '\\.next', 'dist'],
    },
    tsPreCompilationDeps: true,
    tsConfig: {
      fileName: 'tsconfig.base.json',
    },
    enhancedResolveOptions: {
      exportsFields: ['exports'],
      conditionNames: ['import', 'require', 'node', 'default'],
    },
    reporterOptions: {
      text: {
        highlightFocused: true,
      },
    },
  },
};
