/**
 * Dependency Cruiser Configuration - Phase 14.2
 * ==============================================
 *
 * ADR-014: 依賴治理規則
 *
 * Layer Model:
 * - Layer 0: Pages (app/) - 可引用所有
 * - Layer 1: Features (agent/approval/incident/dashboard) - 禁止互相引用
 * - Layer 2: Shared (shared/layout) - 禁止下行引用 Layer 1
 * - Layer 3: Primitives (ui/lib/stores/hooks) - 純工具層
 *
 * @see docs/adr/ADR-014-dependency-governance.md
 */

/** @type {import('dependency-cruiser').IConfiguration} */
module.exports = {
  forbidden: [
    // =========================================================================
    // Layer 1: Feature Isolation (禁止跨 feature 互相引用)
    // =========================================================================
    {
      name: "feature-isolation-agent",
      comment: "agent 元件禁止引用其他 feature (approval/incident/dashboard)",
      severity: "error",
      from: { path: "apps/web/src/components/agent" },
      to: { path: "apps/web/src/components/(approval|incident|dashboard)" }
    },
    {
      name: "feature-isolation-approval",
      comment: "approval 元件禁止引用其他 feature (agent/incident/dashboard)",
      severity: "error",
      from: { path: "apps/web/src/components/approval" },
      to: { path: "apps/web/src/components/(agent|incident|dashboard)" }
    },
    {
      name: "feature-isolation-incident",
      comment: "incident 元件禁止引用其他 feature (agent/approval/dashboard)",
      severity: "error",
      from: { path: "apps/web/src/components/incident" },
      to: { path: "apps/web/src/components/(agent|approval|dashboard)" }
    },
    {
      name: "feature-isolation-dashboard",
      comment: "dashboard 元件禁止引用其他 feature (agent/approval/incident)",
      severity: "error",
      from: { path: "apps/web/src/components/dashboard" },
      to: { path: "apps/web/src/components/(agent|approval|incident)" }
    },

    // =========================================================================
    // Layer 2: Shared Isolation (禁止 shared/ui 下行引用 feature)
    // =========================================================================
    {
      name: "shared-no-feature-import",
      comment: "shared 元件禁止引用 feature 層 (agent/approval/incident/dashboard)",
      severity: "error",
      from: { path: "apps/web/src/components/shared" },
      to: { path: "apps/web/src/components/(agent|approval|incident|dashboard)" }
    },
    {
      name: "ui-no-feature-import",
      comment: "ui 元件禁止引用 feature 層",
      severity: "error",
      from: { path: "apps/web/src/components/ui" },
      to: { path: "apps/web/src/components/(agent|approval|incident|dashboard|shared)" }
    },
    {
      name: "layout-no-feature-import",
      comment: "layout 元件禁止引用 feature 層",
      severity: "error",
      from: { path: "apps/web/src/components/layout" },
      to: { path: "apps/web/src/components/(agent|approval|incident|dashboard)" }
    },

    // =========================================================================
    // Components → App 禁止反向引用
    // =========================================================================
    {
      name: "components-no-app-import",
      comment: "components 禁止引用 app 路由層",
      severity: "error",
      from: { path: "apps/web/src/components" },
      to: { path: "apps/web/src/app" }
    },

    // =========================================================================
    // 禁止循環依賴
    // =========================================================================
    {
      name: "no-circular",
      comment: "禁止循環依賴",
      severity: "error",
      from: {},
      to: {
        circular: true
      }
    },

    // =========================================================================
    // Hooks/Stores/Lib 不應引用 Components (純工具層)
    // =========================================================================
    {
      name: "hooks-no-component-import",
      comment: "hooks 禁止引用 components (純工具層)",
      severity: "warn",
      from: { path: "apps/web/src/hooks" },
      to: { path: "apps/web/src/components" }
    },
    {
      name: "stores-no-component-import",
      comment: "stores 禁止引用 components (純工具層)",
      severity: "warn",
      from: { path: "apps/web/src/stores" },
      to: { path: "apps/web/src/components" }
    },
    {
      name: "lib-no-component-import",
      comment: "lib 禁止引用 components (純工具層)",
      severity: "warn",
      from: { path: "apps/web/src/lib" },
      to: { path: "apps/web/src/components" }
    },

    // =========================================================================
    // #94: Stores 禁止直接 import API Client
    // 原因: 狀態管理層不應直接呼叫 API，應透過 hooks 或 components 層
    // =========================================================================
    {
      name: "stores-no-api-import",
      comment: "stores 禁止直接引用 api-client (應透過 hooks 層)",
      severity: "error",
      from: { path: "apps/web/src/stores" },
      to: { path: "apps/web/src/lib/api-client" }
    }
  ],

  options: {
    doNotFollow: {
      path: "node_modules"
    },

    exclude: {
      path: [
        "node_modules",
        "\\.next",
        "\\.turbo",
        "dist",
        "coverage",
        "__tests__",
        "\\.test\\.",
        "\\.spec\\."
      ]
    },

    includeOnly: {
      path: "apps/web/src"
    },

    // 2026-03-26: 關閉 TS 前置編譯 (避免 monorepo tsconfig extends 問題)
    tsPreCompilationDeps: false,

    enhancedResolveOptions: {
      exportsFields: ["exports"],
      conditionNames: ["import", "require", "node", "default"],
      mainFields: ["main", "types"]
    },

    reporterOptions: {
      dot: {
        collapsePattern: "node_modules/(@[^/]+/[^/]+|[^/]+)"
      },
      text: {
        highlightFocused: true
      }
    }
  }
};
