import type { CommandOption } from "@tui/component/dialog-command"
import { t } from "@/util/i18n"
import { DialogSessionList } from "@tui/component/dialog-session-list"
import { DialogModel } from "@tui/component/dialog-model"
import { DialogAgent } from "@tui/component/dialog-agent"
import { DialogMcp } from "@tui/component/dialog-mcp"
import { DialogVariant } from "@tui/component/dialog-variant"
import { DialogProvider as DialogProviderList } from "@tui/component/dialog-provider"
import { DialogConsoleOrg } from "@tui/component/dialog-console-org"

type Dialog = ReturnType<typeof import("@tui/ui/dialog").useDialog>
type Local = ReturnType<typeof import("@tui/context/local").useLocal>
type Sync = ReturnType<typeof import("@tui/context/sync").useSync>
type Route = ReturnType<typeof import("@tui/context/route").useRoute>
type Connected = ReturnType<typeof import("@tui/component/use-connected").useConnected>

export type CoreCommandDeps = {
  dialog: Dialog
  local: Local
  sync: Sync
  route: Route
  connected: Connected
}

export function registerCoreCommands(deps: CoreCommandDeps): CommandOption[] {
  const { dialog, local, sync, route, connected } = deps
  return [
    {
      title: t("cmd.session.switch"),
      value: "session.list",
      keybind: "session_list",
      category: t("cmd.cat.session"),
      suggested: sync.data.session.length > 0,
      slash: {
        name: "sessions",
        aliases: ["resume", "continue"],
      },
      onSelect: () => {
        dialog.replace(() => <DialogSessionList />)
      },
    },
    {
      title: t("cmd.session.new"),
      suggested: route.data.type === "session",
      value: "session.new",
      keybind: "session_new",
      category: t("cmd.cat.session"),
      slash: {
        name: "new",
        aliases: ["clear"],
      },
      onSelect: () => {
        route.navigate({
          type: "home",
        })
        dialog.clear()
      },
    },
    {
      title: t("cmd.model.switch"),
      value: "model.list",
      keybind: "model_list",
      suggested: true,
      category: t("cmd.cat.agent"),
      slash: {
        name: "models",
      },
      onSelect: () => {
        dialog.replace(() => <DialogModel />)
      },
    },
    {
      title: t("cmd.model.cycle"),
      value: "model.cycle_recent",
      keybind: "model_cycle_recent",
      category: t("cmd.cat.agent"),
      hidden: true,
      onSelect: () => {
        local.model.cycle(1)
      },
    },
    {
      title: t("cmd.model.cycle_reverse"),
      value: "model.cycle_recent_reverse",
      keybind: "model_cycle_recent_reverse",
      category: t("cmd.cat.agent"),
      hidden: true,
      onSelect: () => {
        local.model.cycle(-1)
      },
    },
    {
      title: t("cmd.model.favorite_cycle"),
      value: "model.cycle_favorite",
      keybind: "model_cycle_favorite",
      category: t("cmd.cat.agent"),
      hidden: true,
      onSelect: () => {
        local.model.cycleFavorite(1)
      },
    },
    {
      title: t("cmd.model.favorite_cycle_reverse"),
      value: "model.cycle_favorite_reverse",
      keybind: "model_cycle_favorite_reverse",
      category: t("cmd.cat.agent"),
      hidden: true,
      onSelect: () => {
        local.model.cycleFavorite(-1)
      },
    },
    {
      title: t("cmd.agent.switch"),
      value: "agent.list",
      keybind: "agent_list",
      category: t("cmd.cat.agent"),
      slash: {
        name: "agents",
      },
      onSelect: () => {
        dialog.replace(() => <DialogAgent />)
      },
    },
    {
      title: t("cmd.mcp.toggle"),
      value: "mcp.list",
      category: t("cmd.cat.agent"),
      slash: {
        name: "mcps",
      },
      onSelect: () => {
        dialog.replace(() => <DialogMcp />)
      },
    },
    {
      title: "Refresh MCP Servers",
      value: "mcp.refresh",
      category: t("cmd.cat.agent"),
      slash: {
        name: "mcp-refresh",
      },
      onSelect: async () => {
        await local.mcp.refresh()
      },
    },
    {
      title: t("cmd.agent.cycle"),
      value: "agent.cycle",
      keybind: "agent_cycle",
      category: t("cmd.cat.agent"),
      hidden: true,
      onSelect: () => {
        local.agent.move(1)
      },
    },
    {
      title: t("cmd.variant.cycle"),
      value: "variant.cycle",
      keybind: "variant_cycle",
      category: t("cmd.cat.agent"),
      onSelect: () => {
        local.model.variant.cycle()
      },
    },
    {
      title: t("cmd.variant.switch"),
      value: "variant.list",
      keybind: "variant_list",
      category: t("cmd.cat.agent"),
      hidden: local.model.variant.list().length === 0,
      slash: {
        name: "variants",
      },
      onSelect: () => {
        dialog.replace(() => <DialogVariant />)
      },
    },
    {
      title: t("cmd.agent.cycle_reverse"),
      value: "agent.cycle.reverse",
      keybind: "agent_cycle_reverse",
      category: t("cmd.cat.agent"),
      hidden: true,
      onSelect: () => {
        local.agent.move(-1)
      },
    },
    {
      title: t("cmd.provider.connect"),
      value: "provider.connect",
      suggested: !connected(),
      slash: {
        name: "connect",
      },
      onSelect: () => {
        dialog.replace(() => <DialogProviderList />)
      },
      category: t("cmd.cat.provider"),
    },
    ...(sync.data.console_state.switchableOrgCount > 1
      ? [
          {
            title: t("cmd.provider.switch_org"),
            value: "console.org.switch",
            suggested: Boolean(sync.data.console_state.activeOrgName),
            slash: {
              name: "org",
              aliases: ["orgs", "switch-org"],
            },
            onSelect: () => {
              dialog.replace(() => <DialogConsoleOrg />)
            },
            category: t("cmd.cat.provider"),
          },
        ]
      : []),
  ]
}
