import type { AppRegistryMeta } from '../../AppRegistry';
import { TerminalSquare, Monitor, Columns2, PenLine, Sparkles, ListTree } from 'lucide-react';
import { TerminalWrapper } from './TerminalWrapper';
import { HostTerminalWrapper } from './HostTerminalWrapper';
import { CommandTerminalWrapper } from './CommandTerminalWrapper';
import { TerminalHeader, HostTerminalHeader, TmuxHeader, NvimHeader, ClaudeCodeHeader } from './Headers';
import { RunningShells } from './RunningShells';

export { TerminalView, type TerminalViewProps, type TerminalConnectionState } from './Terminal';

// `new-session -A -s <name>` attaches to that session if it exists and creates it otherwise. Bare `tmux`
// started a brand new session every time, so the panel forgot your windows whenever the shell behind it
// went away — including on a `pm2 restart officer-pty`. Named per panel and derived from panelId, which is
// stable in the saved layout, so this panel always returns to its own session. The tmux server outlives
// the pty, which is what makes this survive things the pty session cannot.
const tmuxSessionName = (panelId: string) => `off-${panelId.replace(/[^a-zA-Z0-9_-]/g, '').slice(0, 24)}`;

const TmuxWrapper = ({ panelId }: { panelId: string }) => (
  <CommandTerminalWrapper
    panelId={panelId}
    command={`tmux new-session -A -s ${tmuxSessionName(panelId)}`}
    statePrefix="tmux"
  />
);

const NvimWrapper = ({ panelId }: { panelId: string }) => (
  <CommandTerminalWrapper panelId={panelId} command="nvim" statePrefix="nvim" />
);

// No turn-complete refresh here. Claude's Stop hook was supposed to drive one, but it was written into the
// managed home under DATA_PATH while HOME_DIR points terminals at the owner's real login home — so it was
// never installed and never fired. The Chat panel does the same job from onTurnComplete, in-process.
const ClaudeCodeWrapper = ({ panelId }: { panelId: string }) => (
  <CommandTerminalWrapper panelId={panelId} command="claude --dangerously-skip-permissions" statePrefix="claude-code" />
);

// zoomable: false on every real terminal — xterm sizes its own font and reflows to a measured cell grid,
// so a wrapping CSS scale both blurs the glyphs and desyncs the cols/rows it reports to the pty. The
// per-terminal font-size control in the panel header is the zoom for these.
export const appRegistryMetas: AppRegistryMeta[] = [
  {
    key: 'officerdev/terminal',
    name: 'Terminal',
    icon: TerminalSquare,
    component: TerminalWrapper,
    header: TerminalHeader,
    zoomable: false,
  },
  {
    key: 'officerdev/terminal-host',
    name: 'Host Terminal',
    icon: Monitor,
    component: HostTerminalWrapper,
    header: HostTerminalHeader,
    availableOnPanel: false,
    zoomable: false,
  },
  {
    key: 'officerdev/tmux',
    name: 'Tmux',
    icon: Columns2,
    component: TmuxWrapper,
    header: TmuxHeader,
    zoomable: false,
  },
  {
    key: 'officerdev/nvim',
    name: 'Neovim',
    icon: PenLine,
    component: NvimWrapper,
    header: NvimHeader,
    zoomable: false,
  },
  {
    key: 'officerdev/running-shells',
    name: 'Running Shells',
    icon: ListTree,
    component: RunningShells,
  },
  {
    key: 'officerdev/claude-code',
    name: 'Claude Code',
    icon: Sparkles,
    component: ClaudeCodeWrapper,
    header: ClaudeCodeHeader,
    zoomable: false,
  },
];
