import type { Command } from 'commander';
import readline from 'readline';
import { loadStoredZaiAuth, storeZaiAuth } from '../auth/index.js';

type ZaiAuthMode = 'api' | 'plan';

async function promptHidden(question: string): Promise<string> {
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
  const rlAny = rl as unknown as { stdoutMuted?: boolean; _writeToOutput?: (s: string) => void };
  rlAny.stdoutMuted = true;

  rlAny._writeToOutput = (s: string) => {
    if (!rlAny.stdoutMuted) {
      process.stdout.write(s);
      return;
    }
    // Mask input characters, but preserve newlines.
    if (s.includes('\n')) {
      process.stdout.write('\n');
    } else {
      process.stdout.write('*');
    }
  };

  const answer = await new Promise<string>((resolve) => rl.question(question, resolve));
  rlAny.stdoutMuted = false;
  rl.close();
  process.stdout.write('\n');
  return answer.trim();
}

async function promptYesNo(question: string): Promise<boolean> {
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
  const answer = await new Promise<string>((resolve) => rl.question(question, resolve));
  rl.close();
  const normalized = answer.trim().toLowerCase();
  return normalized === 'y' || normalized === 'yes';
}

async function promptText(question: string): Promise<string> {
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
  const answer = await new Promise<string>((resolve) => rl.question(question, resolve));
  rl.close();
  return answer.trim();
}

function parseZaiAuthMode(value: string): ZaiAuthMode {
  const mode = value.trim().toLowerCase();
  if (mode === 'api' || mode === 'plan') {
    return mode;
  }
  throw new Error(`Invalid mode "${value}". Expected: api or plan.`);
}

async function resolveZaiAuthMode(mode?: ZaiAuthMode): Promise<ZaiAuthMode> {
  if (mode) {
    return mode;
  }

  console.log('Choose Z.AI auth mode:');
  console.log('  1) API (standard API endpoint)');
  console.log('  2) Coding Plan (coding endpoint)');
  const choice = (await promptText('Select [1-2] (default 1): ')).toLowerCase();
  return choice === '2' || choice === 'plan' ? 'plan' : 'api';
}

export function registerZaiAuthCommand(program: Command): void {
  program
    .command('zai-auth')
    .description('Store a Z.AI API key for the zhipuai provider (auth.json)')
    .option('--mode <mode>', 'Credential mode: api or plan', parseZaiAuthMode)
    .action(async (opts: { mode?: ZaiAuthMode }) => {
      const existing = loadStoredZaiAuth();
      if (existing) {
        console.log('Z.AI credential already exists.');
        const confirmed = await promptYesNo('Re-authenticate and replace it? (y/N): ');
        if (!confirmed) {
          console.log('Cancelled.');
          process.exit(0);
        }
      }

      const mode = await resolveZaiAuthMode(opts.mode);

      console.log('Z.AI uses API keys (HTTP Bearer), not an OAuth device flow.');
      console.log('Create a key at: https://z.ai/manage-apikey/apikey-list');
      console.log('');

      try {
        const apiKey = await promptHidden('Enter Z.AI API key: ');
        storeZaiAuth(apiKey);
        console.log('');
        console.log('Z.AI credential stored in ~/.config/flynn/auth.json');
        console.log('');
        if (mode === 'plan') {
          console.log('Mode: Coding Plan');
          console.log('Set model endpoint to: https://api.z.ai/api/coding/paas/v4');
        } else {
          console.log('Mode: API');
          console.log('Set model endpoint to: https://api.z.ai/api/paas/v4');
        }
      } catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        console.error(`Z.AI auth failed: ${message}`);
        process.exit(1);
      }
    });
}
