import { Command } from 'commander';
import { callDaemon } from '../client/http-client.js';
import { formatOutput } from '../output/formatter.js';

export function registerStateCommands(program: Command) {
  program
    .command('cookie-list')
    .description('列出 cookie')
    .requiredOption('--session <name>')
    .option('--domain <d>')
    .option('--raw')
    .action(async (opts) => {
      const r = await callDaemon('/admin/browser-daemon/cookie-list', { sessionName: opts.session, domain: opts.domain });
      console.log(formatOutput(r, opts.raw));
    });

  program
    .command('state-save')
    .description('保存登录态')
    .requiredOption('--session <name>')
    .option('--output <path>', '输出文件路径')
    .option('--raw')
    .action(async (opts) => {
      const r = await callDaemon('/admin/browser-daemon/state-save', { sessionName: opts.session, filePath: opts.output });
      console.log(formatOutput(r, opts.raw));
    });

  program
    .command('state-load')
    .description('恢复登录态')
    .requiredOption('--session <name>')
    .requiredOption('--input <path>', '输入文件路径')
    .option('--raw')
    .action(async (opts) => {
      await callDaemon('/admin/browser-daemon/state-load', { sessionName: opts.session, filePath: opts.input });
      console.log(formatOutput({ loaded: opts.input }, opts.raw));
    });
}
