import type { PaneDefinition, WorkspaceDefinition, WorkspacePreset, WorkspacePresetId } from '@/types/workbench';

function pane(
  role: PaneDefinition['role'],
  title: string,
  rootPath: string,
  startupCommand: string | undefined,
  isMain: boolean,
  sizeWeight: number,
): PaneDefinition {
  return {
    id: crypto.randomUUID(),
    role,
    title,
    cwd: rootPath,
    startupCommand,
    isMain,
    sizeWeight,
  };
}

function workspaceFromPreset(
  preset: WorkspacePresetId,
  name: string,
  rootPath: string,
  panes: PaneDefinition[],
  direction: 'horizontal' | 'vertical' = 'horizontal',
): WorkspaceDefinition {
  return {
    id: crypto.randomUUID(),
    name,
    rootPath,
    preset,
    layout: { direction, panes },
    vigthoria: {
      debugMode: false,
      autoApprove: false,
      autoSave: true,
    },
    updatedAt: String(Date.now()),
  };
}

export const WORKSPACE_PRESETS: WorkspacePreset[] = [
  {
    id: 'agentSolo',
    name: 'V3 Agent Solo',
    description: 'Single V3 main agent terminal for focused analysis.',
    build: (name, rootPath) =>
      workspaceFromPreset('agentSolo', name, rootPath, [
        pane('mainAgent', 'V3 Main Agent', rootPath, 'vigthoria agent', true, 1),
      ]),
  },
  {
    id: 'agentDev',
    name: 'V3 Agent + Dev',
    description: 'V3 main agent with a dev server pane (recommended for games).',
    build: (name, rootPath) =>
      workspaceFromPreset('agentDev', name, rootPath, [
        pane('mainAgent', 'V3 Main Agent', rootPath, 'vigthoria agent', true, 0.62),
        pane('dev', 'Dev Server', rootPath, 'npm run dev', false, 0.38),
      ]),
  },
  {
    id: 'fullStack',
    name: 'V3 Full Stack',
    description: 'V3 agent, dev server, and git shell in one layout.',
    build: (name, rootPath) =>
      workspaceFromPreset('fullStack', name, rootPath, [
        pane('mainAgent', 'V3 Main Agent', rootPath, 'vigthoria agent', true, 0.5),
        pane('dev', 'Dev Server', rootPath, 'npm run dev', false, 0.25),
        pane('git', 'Git / Shell', rootPath, undefined, false, 0.25),
      ]),
  },
  {
    id: 'analysis',
    name: 'V3 Deep Analysis',
    description: 'V3 agent-focused layout with report panel for architecture reviews.',
    build: (name, rootPath) =>
      workspaceFromPreset('analysis', name, rootPath, [
        pane('mainAgent', 'V3 Main Agent', rootPath, 'vigthoria agent', true, 0.7),
        pane('shell', 'Support Shell', rootPath, undefined, false, 0.3),
      ]),
  },
  {
    id: 'v4Operating',
    name: 'V4 Operating Agent',
    description: 'Single-pane layout that launches the V4 operating agent (separate DeerFlow-based runtime).',
    build: (name, rootPath, options) => {
      const provider = options?.v4Provider ?? 'v3_local';
      const command = `vigthoria v4 --non-interactive --provider ${provider}`;
      return workspaceFromPreset('v4Operating', name, rootPath, [
        pane('mainAgent', 'V4 Operating Agent', rootPath, command, true, 1),
      ]);
    },
  },
];

export function getPreset(id: WorkspacePresetId): WorkspacePreset {
  return WORKSPACE_PRESETS.find((p) => p.id === id) ?? WORKSPACE_PRESETS[1];
}
