import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';

// Mock the accounts module so the dynamic import is intercepted
vi.mock('../agent/accounts/index.js', () => ({
  extractCurrentClaudeAccount: vi.fn(),
}));

import { extractCurrentClaudeAccount } from '../agent/accounts/index.js';
const mockExtract = vi.mocked(extractCurrentClaudeAccount);

import { createCli } from './index.js';

describe('cw account extract', () => {
  beforeEach(() => {
    vi.spyOn(console, 'log').mockImplementation(() => {});
    vi.spyOn(console, 'error').mockImplementation(() => {});
    vi.spyOn(process, 'exit').mockImplementation((_code?: string | number | null) => undefined as never);
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('happy path — no email flag prints JSON to stdout', async () => {
    mockExtract.mockResolvedValueOnce({
      email: 'user@example.com',
      accountUuid: 'uuid-1',
      configJson: { hasCompletedOnboarding: true },
      credentials: '{"claudeAiOauth":{"accessToken":"tok"}}',
    });

    const program = createCli();
    await program.parseAsync(['node', 'cw', 'account', 'extract']);

    expect(console.log).toHaveBeenCalledTimes(1);
    const output = JSON.parse((console.log as ReturnType<typeof vi.fn>).mock.calls[0][0]);
    expect(output.email).toBe('user@example.com');
    expect(output.configJson).toBe('{"hasCompletedOnboarding":true}');
    expect(output.credentials).toBe('{"claudeAiOauth":{"accessToken":"tok"}}');
    expect(process.exit).not.toHaveBeenCalled();
  });

  it('happy path — matching email flag succeeds', async () => {
    mockExtract.mockResolvedValueOnce({
      email: 'user@example.com',
      accountUuid: 'uuid-1',
      configJson: { hasCompletedOnboarding: true },
      credentials: '{"claudeAiOauth":{"accessToken":"tok"}}',
    });

    const program = createCli();
    await program.parseAsync(['node', 'cw', 'account', 'extract', '--email', 'user@example.com']);

    expect(console.log).toHaveBeenCalledTimes(1);
    const output = JSON.parse((console.log as ReturnType<typeof vi.fn>).mock.calls[0][0]);
    expect(output.email).toBe('user@example.com');
    expect(output.configJson).toBe('{"hasCompletedOnboarding":true}');
    expect(output.credentials).toBe('{"claudeAiOauth":{"accessToken":"tok"}}');
    expect(process.exit).not.toHaveBeenCalled();
  });

  it('email mismatch prints error and exits with code 1', async () => {
    mockExtract.mockResolvedValueOnce({
      email: 'other@example.com',
      accountUuid: 'uuid-2',
      configJson: { hasCompletedOnboarding: true },
      credentials: '{"claudeAiOauth":{"accessToken":"tok"}}',
    });

    const program = createCli();
    await program.parseAsync(['node', 'cw', 'account', 'extract', '--email', 'user@example.com']);

    expect(console.error).toHaveBeenCalledWith(
      "Account 'user@example.com' not found (active account is 'other@example.com')"
    );
    expect(process.exit).toHaveBeenCalledWith(1);
    expect(console.log).not.toHaveBeenCalled();
  });

  it('extractCurrentClaudeAccount throws prints error and exits with code 1', async () => {
    mockExtract.mockRejectedValueOnce(new Error('No Claude account found'));

    const program = createCli();
    await program.parseAsync(['node', 'cw', 'account', 'extract']);

    expect(console.error).toHaveBeenCalledWith('Failed to extract account:', 'No Claude account found');
    expect(process.exit).toHaveBeenCalledWith(1);
    expect(console.log).not.toHaveBeenCalled();
  });
});
