import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as qenv from '@push.rocks/qenv';
import * as fs from 'fs';
import * as path from 'path';
import { getModel } from '../ts/index.js';
import { analyzeImage } from '../ts_vision/index.js';

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

tap.test('analyzeImage should describe a test image', async () => {
  const apiKey = await testQenv.getEnvVarOnDemand('ANTHROPIC_TOKEN');
  if (!apiKey) {
    console.log('ANTHROPIC_TOKEN not set, skipping test');
    return;
  }

  // Find an image file recursively in testimages/
  const testImageDir = path.join(process.cwd(), 'test', 'testimages');
  if (!fs.existsSync(testImageDir)) {
    console.log('No test images directory found, skipping test');
    return;
  }

  const findImage = (dir: string): string | null => {
    for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
      const fullPath = path.join(dir, entry.name);
      if (entry.isDirectory()) {
        const found = findImage(fullPath);
        if (found) return found;
      } else if (/\.(jpg|jpeg|png)$/i.test(entry.name)) {
        return fullPath;
      }
    }
    return null;
  };

  const imagePath = findImage(testImageDir);
  if (!imagePath) {
    console.log('No test images found, skipping test');
    return;
  }

  const imageBuffer = fs.readFileSync(imagePath);
  const ext = path.extname(imagePath).toLowerCase();
  const mediaType = ext === '.png' ? 'image/png' : 'image/jpeg';

  const model = getModel({
    provider: 'anthropic',
    model: 'claude-sonnet-4-5-20250929',
    apiKey,
    promptCaching: false,
  });

  const result = await analyzeImage({
    model,
    image: imageBuffer,
    prompt: 'Describe this image briefly.',
    mediaType: mediaType as 'image/jpeg' | 'image/png',
  });

  console.log('Vision result:', result);
  expect(result).toBeTruthy();
  expect(result.length).toBeGreaterThan(10);
});

export default tap.start();
