// Matrix App Launcher 九宮格清單
// 來源：matrix/identity/.agents/specs/identity/apps.json（v0 過渡複製，未來 v1 抽進 @matrix/identity-ui）
// 規範：matrix/identity/.agents/specs/identity/design.md §2.5

export type AppEntry = {
  id: string;
  name: string;
  url: string;
  icon?: string;
  description?: string;
  access?: 'public' | 'allowlist';
  allowlist_emails?: string[];
  locked_tooltip?: string;
};

export const MATRIX_APPS: AppEntry[] = [
  {
    id: 'arcrun',
    name: 'Arcrun',
    url: 'https://arcrun.dev',
    icon: '🔄',
    description: '工作流引擎與零件平台',
  },
  {
    id: 'dashboard',
    name: 'Dashboard',
    url: 'https://arcrun.dev/dashboard',
    icon: '🔑',
    description: 'API Key 管理',
  },
  {
    id: 'integrations',
    name: 'Integrations',
    url: 'https://arcrun.dev/integrations',
    icon: '🧩',
    description: '服務目錄',
  },
  {
    id: 'mira',
    name: 'Mira',
    url: 'https://arcrun.dev/mira',
    icon: '🌊',
    description: '個人化 KM 河道',
    access: 'allowlist',
    // 白名單不寫死進 source（公開 repo 不帶個人 email）；build 時由 env 注入，逗號分隔
    allowlist_emails: (process.env.NEXT_PUBLIC_MIRA_ALLOWLIST ?? '').split(',').filter(Boolean),
    locked_tooltip: '即將開放',
  },
];

export function isAppAccessible(app: AppEntry, userEmail: string | null): boolean {
  if (app.access !== 'allowlist') return true;
  if (!userEmail) return false;
  return (app.allowlist_emails ?? []).includes(userEmail);
}
