import type { Addon, Tier } from "./types"
import { core } from "./addon-core"
import { llm } from "./addon-llm"
import { lifecycle } from "./addon-lifecycle"
import { server } from "./addon-server"
import { tools } from "./addon-tools"
import { data } from "./addon-data"
import { dev } from "./addon-dev"
import { web } from "./addon-web"
import { remote } from "./addon-remote"
import { analytics } from "./addon-analytics"

// ╔══════════════════════════════════════════════════════════════════╗
// ║  애드온 레지스트리                                               ║
// ║                                                                   ║
// ║  LIGHT  = core + llm + lifecycle              (3 addons, 10 cmd)  ║
// ║  CODE   = LIGHT + server + tools + data + dev (8 addons, 20 cmd) ║
// ║  MASTER = CODE + web + remote + analytics     (11 addons, 24 cmd)║
// ╚══════════════════════════════════════════════════════════════════╝

export const allAddons: Addon[] = [
  // LIGHT
  core,        // TUI 채팅, run, generate, session
  llm,         // init, auth, models, config
  lifecycle,   // upgrade, uninstall

  // CODE
  server,      // serve, attach, acp
  tools,       // mcp, plugin
  data,        // export, import, db
  dev,         // agent, debug

  // MASTER
  web,         // web UI
  remote,      // real-time relay
  analytics,   // stats, pr, roll-call
]

const tierPriority: Record<Tier, number> = { light: 0, code: 1, master: 2 }

export function addonsForTier(tier: Tier): Addon[] {
  return allAddons.filter((a) => tierPriority[tier] >= tierPriority[a.minTier])
}

export async function commandsForTier(tier: Tier): Promise<any[]> {
  const addons = addonsForTier(tier)
  const commands: any[] = []
  for (const addon of addons) {
    for (const loader of addon.commands) {
      const mod = await loader()
      commands.push(...Object.values(mod))
    }
  }
  return commands
}

export function manifest(tier: Tier) {
  const addons = addonsForTier(tier)
  return addons.map((a) => ({
    id: a.id,
    category: a.category,
    commands: a.commands.length,
    required: a.required,
  }))
}
