import type { Prompter } from './prompts.js';
import type { ConfigBuilder } from './config.js';

interface ProviderDef {
  name: string;
  provider: string;
  defaultModel: string;
  fastModel?: string;
  needsApiKey: boolean;
  needsEndpoint: boolean;
  defaultEndpoint?: string;
  apiKeyLabel?: string;
}

const TOP_TIER: ProviderDef[] = [
  { name: 'Anthropic', provider: 'anthropic', defaultModel: 'claude-sonnet-4-20250514', fastModel: 'claude-haiku-4-5-20251001', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'Anthropic API key' },
  { name: 'OpenAI', provider: 'openai', defaultModel: 'gpt-4.1', fastModel: 'gpt-4.1-mini', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'OpenAI API key' },
  { name: 'Ollama (local)', provider: 'ollama', defaultModel: 'llama3.3', fastModel: 'llama3.2:3b', needsApiKey: false, needsEndpoint: true, defaultEndpoint: 'http://localhost:11434' },
];

const SECOND_TIER: ProviderDef[] = [
  { name: 'Gemini', provider: 'gemini', defaultModel: 'gemini-2.5-flash', fastModel: 'gemini-2.0-flash-lite', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'Gemini API key' },
  { name: 'OpenRouter', provider: 'openrouter', defaultModel: 'anthropic/claude-sonnet-4', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'OpenRouter API key' },
  { name: 'Vercel AI Gateway', provider: 'vercel', defaultModel: 'openai/gpt-4.1', fastModel: 'openai/gpt-4.1-mini', needsApiKey: true, needsEndpoint: true, defaultEndpoint: 'https://ai-gateway.vercel.sh/v1', apiKeyLabel: 'Vercel AI Gateway API key' },
  { name: 'Z.AI (GLM)', provider: 'zhipuai', defaultModel: 'glm-4.7', needsApiKey: true, needsEndpoint: true, defaultEndpoint: 'https://api.z.ai/api/paas/v4', apiKeyLabel: 'Z.AI API key' },
  { name: 'xAI (Grok)', provider: 'xai', defaultModel: 'grok-3', fastModel: 'grok-3-mini', needsApiKey: true, needsEndpoint: false, apiKeyLabel: 'xAI API key' },
  { name: 'Amazon Bedrock', provider: 'bedrock', defaultModel: 'anthropic.claude-sonnet-4-20250514-v1:0', needsApiKey: false, needsEndpoint: false },
  { name: 'GitHub Models', provider: 'github', defaultModel: 'claude-sonnet-4-20250514', needsApiKey: false, needsEndpoint: false },
];

const PROVIDER_HELP: Record<string, string> = {
  anthropic: 'Get your API key at https://console.anthropic.com/settings/keys',
  openai: 'Get your API key at https://platform.openai.com/api-keys',
  ollama: 'Ollama runs locally — install from https://ollama.com and run: ollama serve',
  gemini: 'Get your API key at https://aistudio.google.com/apikey',
  openrouter: 'Get your API key at https://openrouter.ai/keys (supports 200+ models)',
  vercel: 'Vercel AI Gateway uses an API key (AI_GATEWAY_API_KEY) and an OpenAI-compatible base URL (default: https://ai-gateway.vercel.sh/v1)',
  zhipuai: 'Get your API key at https://z.ai/manage-apikey/apikey-list (Coding Plan endpoint: https://api.z.ai/api/coding/paas/v4)',
  xai: 'Get your API key at https://console.x.ai',
  bedrock: 'Uses AWS credentials from environment (~/.aws/credentials or IAM role)',
  github: 'Uses GitHub Copilot — authenticate via OAuth device flow on first use',
};

async function configureProvider(p: Prompter, def: ProviderDef): Promise<{
  provider: string; model: string; api_key?: string; endpoint?: string;
}> {
  const help = PROVIDER_HELP[def.provider];
  if (help) {p.println(`  ${help}`);}

  const config: Record<string, string> = { provider: def.provider };
  if (def.needsApiKey) {config.api_key = await p.password(def.apiKeyLabel ?? 'API key');}
  if (def.needsEndpoint) {config.endpoint = await p.ask('Host', def.defaultEndpoint);}
  config.model = await p.ask('Model', def.defaultModel);
  return config as { provider: string; model: string; api_key?: string; endpoint?: string };
}

export async function setupProviders(p: Prompter, builder: ConfigBuilder): Promise<void> {
  const allOptions = [
    ...TOP_TIER.map(d => ({ label: d.name, value: d })),
    { label: 'More providers...', value: null as ProviderDef | null },
  ];

  let chosen: ProviderDef;
  const selection = await p.choose('Model provider:', allOptions);

  if (selection === null) {
    const secondOptions = SECOND_TIER.map(d => ({ label: d.name, value: d }));
    chosen = await p.choose('Model provider:', secondOptions);
  } else {
    chosen = selection;
  }

  p.println();
  const cfg = await configureProvider(p, chosen);
  builder.setProvider('default', cfg);

  if (chosen.fastModel) {
    p.println();
    const wantFast = await p.confirm('Configure a fast tier for compaction/delegation?', false);
    if (wantFast) {
      const fastModel = await p.ask('Fast model', chosen.fastModel);
      builder.setProvider('fast', { ...cfg, model: fastModel });
    }
  }
}
