import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as qenv from '@push.rocks/qenv';

const testQenv = new qenv.Qenv('./', './.nogit/');

import * as smartai from '../ts/index.js';

let anthropicProviderQuick: smartai.AnthropicProvider;
let anthropicProviderNormal: smartai.AnthropicProvider;
let anthropicProviderDeep: smartai.AnthropicProvider;
let anthropicProviderOff: smartai.AnthropicProvider;

// Test 'quick' mode
tap.test('Extended Thinking: should create Anthropic provider with quick mode', async () => {
  anthropicProviderQuick = new smartai.AnthropicProvider({
    anthropicToken: await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN'),
    extendedThinking: 'quick',
  });
  await anthropicProviderQuick.start();
  expect(anthropicProviderQuick).toBeInstanceOf(smartai.AnthropicProvider);
});

tap.test('Extended Thinking: should chat with quick mode (2048 tokens)', async () => {
  const userMessage = 'Explain quantum entanglement in simple terms.';
  const response = await anthropicProviderQuick.chat({
    systemMessage: 'You are a helpful physics teacher.',
    userMessage: userMessage,
    messageHistory: [],
  });
  console.log(`Quick Mode - User: ${userMessage}`);
  console.log(`Quick Mode - Response length: ${response.message.length} chars`);
  expect(response.role).toEqual('assistant');
  expect(response.message).toBeTruthy();
  expect(response.message.toLowerCase()).toInclude('quantum');
});

tap.test('Extended Thinking: should stop quick mode provider', async () => {
  await anthropicProviderQuick.stop();
});

// Test 'normal' mode (default)
tap.test('Extended Thinking: should create Anthropic provider with normal mode (default)', async () => {
  anthropicProviderNormal = new smartai.AnthropicProvider({
    anthropicToken: await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN'),
    // extendedThinking not specified, should default to 'normal'
  });
  await anthropicProviderNormal.start();
  expect(anthropicProviderNormal).toBeInstanceOf(smartai.AnthropicProvider);
});

tap.test('Extended Thinking: should chat with normal mode (8000 tokens default)', async () => {
  const userMessage = 'What are the implications of the P vs NP problem?';
  const response = await anthropicProviderNormal.chat({
    systemMessage: 'You are a helpful computer science expert.',
    userMessage: userMessage,
    messageHistory: [],
  });
  console.log(`Normal Mode - User: ${userMessage}`);
  console.log(`Normal Mode - Response length: ${response.message.length} chars`);
  expect(response.role).toEqual('assistant');
  expect(response.message).toBeTruthy();
  expect(response.message.length).toBeGreaterThan(50);
});

tap.test('Extended Thinking: should stop normal mode provider', async () => {
  await anthropicProviderNormal.stop();
});

// Test 'deep' mode
tap.test('Extended Thinking: should create Anthropic provider with deep mode', async () => {
  anthropicProviderDeep = new smartai.AnthropicProvider({
    anthropicToken: await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN'),
    extendedThinking: 'deep',
  });
  await anthropicProviderDeep.start();
  expect(anthropicProviderDeep).toBeInstanceOf(smartai.AnthropicProvider);
});

tap.test('Extended Thinking: should chat with deep mode (16000 tokens)', async () => {
  const userMessage = 'Analyze the philosophical implications of artificial consciousness.';
  const response = await anthropicProviderDeep.chat({
    systemMessage: 'You are a philosopher and cognitive scientist.',
    userMessage: userMessage,
    messageHistory: [],
  });
  console.log(`Deep Mode - User: ${userMessage}`);
  console.log(`Deep Mode - Response length: ${response.message.length} chars`);
  expect(response.role).toEqual('assistant');
  expect(response.message).toBeTruthy();
  expect(response.message.length).toBeGreaterThan(100);
});

tap.test('Extended Thinking: should stop deep mode provider', async () => {
  await anthropicProviderDeep.stop();
});

// Test 'off' mode
tap.test('Extended Thinking: should create Anthropic provider with thinking disabled', async () => {
  anthropicProviderOff = new smartai.AnthropicProvider({
    anthropicToken: await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN'),
    extendedThinking: 'off',
  });
  await anthropicProviderOff.start();
  expect(anthropicProviderOff).toBeInstanceOf(smartai.AnthropicProvider);
});

tap.test('Extended Thinking: should chat with thinking disabled', async () => {
  const userMessage = 'What is 2 + 2?';
  const response = await anthropicProviderOff.chat({
    systemMessage: 'You are a helpful assistant.',
    userMessage: userMessage,
    messageHistory: [],
  });
  console.log(`Thinking Off - User: ${userMessage}`);
  console.log(`Thinking Off - Response: ${response.message}`);
  expect(response.role).toEqual('assistant');
  expect(response.message).toBeTruthy();
  expect(response.message).toInclude('4');
});

tap.test('Extended Thinking: should stop off mode provider', async () => {
  await anthropicProviderOff.stop();
});

// Test with vision method
tap.test('Extended Thinking: should work with vision method', async () => {
  const provider = new smartai.AnthropicProvider({
    anthropicToken: await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN'),
    extendedThinking: 'normal',
  });
  await provider.start();

  // Create a simple test image (1x1 red pixel PNG)
  const redPixelPng = Buffer.from(
    'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==',
    'base64'
  );

  const response = await provider.vision({
    image: redPixelPng,
    prompt: 'What color is this image?',
  });

  console.log(`Vision with Thinking - Response: ${response}`);
  expect(response).toBeTruthy();
  expect(response.toLowerCase()).toInclude('red');

  await provider.stop();
});

export default tap.start();
