/**
 * Test for Talking Heads Plugin
 * Verifies plugin structure and core functionality
 */

const path = require('path');
const fs = require('fs');

// Test plugin structure
console.log('🧪 Testing Talking Heads Plugin Structure...\n');

const pluginDir = path.join(__dirname, '../plugins/talking-heads');

// Test 1: Plugin directory exists
console.log('Test 1: Plugin directory exists');
if (fs.existsSync(pluginDir)) {
  console.log('✅ PASS: Plugin directory found\n');
} else {
  console.log('❌ FAIL: Plugin directory not found\n');
  process.exit(1);
}

// Test 2: plugin.json exists and is valid
console.log('Test 2: plugin.json exists and is valid');
const pluginJsonPath = path.join(pluginDir, 'plugin.json');
try {
  const pluginJson = JSON.parse(fs.readFileSync(pluginJsonPath, 'utf8'));
  console.log('✅ PASS: plugin.json is valid');
  console.log(`   - ID: ${pluginJson.id}`);
  console.log(`   - Name: ${pluginJson.name}`);
  console.log(`   - Version: ${pluginJson.version}`);
  console.log(`   - Entry: ${pluginJson.entry}\n`);
} catch (error) {
  console.log('❌ FAIL: plugin.json is invalid:', error.message, '\n');
  process.exit(1);
}

// Test 3: Main entry file exists
console.log('Test 3: Main entry file exists');
const mainJsPath = path.join(pluginDir, 'main.js');
if (fs.existsSync(mainJsPath)) {
  console.log('✅ PASS: main.js found\n');
} else {
  console.log('❌ FAIL: main.js not found\n');
  process.exit(1);
}

// Test 4: Required engine files exist
console.log('Test 4: Required engine files exist');
const engineFiles = [
  'engines/avatar-generator.js',
  'engines/sprite-generator.js',
  'engines/animation-controller.js'
];

let enginesPassed = true;
for (const file of engineFiles) {
  const filePath = path.join(pluginDir, file);
  if (fs.existsSync(filePath)) {
    console.log(`   ✅ ${file}`);
  } else {
    console.log(`   ❌ ${file} - NOT FOUND`);
    enginesPassed = false;
  }
}
console.log(enginesPassed ? '✅ PASS: All engine files exist\n' : '❌ FAIL: Some engine files missing\n');

// Test 5: Required utility files exist
console.log('Test 5: Required utility files exist');
const utilFiles = [
  'utils/cache-manager.js',
  'utils/role-manager.js',
  'utils/style-templates.js'
];

let utilsPassed = true;
for (const file of utilFiles) {
  const filePath = path.join(pluginDir, file);
  if (fs.existsSync(filePath)) {
    console.log(`   ✅ ${file}`);
  } else {
    console.log(`   ❌ ${file} - NOT FOUND`);
    utilsPassed = false;
  }
}
console.log(utilsPassed ? '✅ PASS: All utility files exist\n' : '❌ FAIL: Some utility files missing\n');

// Test 6: UI files exist
console.log('Test 6: UI files exist');
const uiFiles = [
  'ui.html',
  'assets/ui.css',
  'assets/ui.js'
];

let uiPassed = true;
for (const file of uiFiles) {
  const filePath = path.join(pluginDir, file);
  if (fs.existsSync(filePath)) {
    console.log(`   ✅ ${file}`);
  } else {
    console.log(`   ❌ ${file} - NOT FOUND`);
    uiPassed = false;
  }
}
console.log(uiPassed ? '✅ PASS: All UI files exist\n' : '❌ FAIL: Some UI files missing\n');

// Test 7: Overlay files exist
console.log('Test 7: Overlay files exist');
const overlayFiles = [
  'overlay.html',
  'assets/overlay.css',
  'assets/overlay.js'
];

let overlayPassed = true;
for (const file of overlayFiles) {
  const filePath = path.join(pluginDir, file);
  if (fs.existsSync(filePath)) {
    console.log(`   ✅ ${file}`);
  } else {
    console.log(`   ❌ ${file} - NOT FOUND`);
    overlayPassed = false;
  }
}
console.log(overlayPassed ? '✅ PASS: All overlay files exist\n' : '❌ FAIL: Some overlay files missing\n');

// Test 8: Style templates module loads correctly
console.log('Test 8: Style templates module loads correctly');
try {
  const styleTemplates = require(path.join(pluginDir, 'utils/style-templates.js'));
  const templates = styleTemplates.getAllStyleTemplates();
  const keys = styleTemplates.getStyleKeys();
  
  console.log(`   ✅ Module loaded`);
  console.log(`   ✅ Found ${keys.length} style templates: ${keys.join(', ')}`);
  
  // Verify all expected styles exist
  const expectedStyles = ['furry', 'tech', 'medieval', 'noble', 'cartoon', 'whimsical', 'realistic'];
  const allExist = expectedStyles.every(style => keys.includes(style));
  
  if (allExist) {
    console.log('✅ PASS: All expected style templates present\n');
  } else {
    console.log('❌ FAIL: Some style templates missing\n');
  }
} catch (error) {
  console.log('❌ FAIL: Style templates module error:', error.message, '\n');
}

// Test 9: Main plugin class loads correctly
console.log('Test 9: Main plugin class can be loaded');
try {
  const TalkingHeadsPlugin = require(mainJsPath);
  console.log('✅ PASS: Main plugin class loaded successfully\n');
  
  // Check if it's a class/constructor
  if (typeof TalkingHeadsPlugin === 'function') {
    console.log('   ✅ Plugin is a valid constructor function\n');
  } else {
    console.log('   ⚠️  WARNING: Plugin may not be a valid constructor\n');
  }
} catch (error) {
  console.log('❌ FAIL: Main plugin class error:', error.message, '\n');
}

// Test 10: README exists
console.log('Test 10: README documentation exists');
const readmePath = path.join(pluginDir, 'README.md');
if (fs.existsSync(readmePath)) {
  const readmeSize = fs.statSync(readmePath).size;
  console.log(`✅ PASS: README.md found (${readmeSize} bytes)\n`);
} else {
  console.log('❌ FAIL: README.md not found\n');
}

// Test 11: Default config exposes OpenAI provider fields
console.log('Test 11: Default config exposes OpenAI provider fields');
try {
  const mockApi = {
    getConfig: () => null,
    setConfig: () => {},
    getSocketIO: () => ({ on: () => {} }),
    getDatabase: () => ({ getSetting: () => null }),
    getPluginDataDir: () => '/tmp',
    ensurePluginDataDir: async () => {},
    registerRoute: () => {},
    registerSocket: () => {},
    registerTikTokEvent: () => {},
    logger: { info() {}, warn() {}, error() {}, debug() {} }
  };
  const TalkingHeadsPlugin = require(mainJsPath);
  const pluginInstance = new TalkingHeadsPlugin(mockApi);

  if (pluginInstance.config.imageProvider && pluginInstance.config.openaiImageModel) {
    console.log('✅ PASS: OpenAI image provider defaults are present\n');
  } else {
    console.log('❌ FAIL: OpenAI image provider defaults missing\n');
    process.exit(1);
  }
} catch (error) {
  console.log('❌ FAIL: Could not verify OpenAI defaults:', error.message, '\n');
  process.exit(1);
}

// Summary
console.log('\n' + '='.repeat(50));
console.log('📊 Test Summary');
console.log('='.repeat(50));
console.log('✅ Plugin structure is valid and complete!');
console.log('✅ All core files are present');
console.log('✅ All modules can be loaded');
console.log('\n🎉 Talking Heads Plugin is ready for use!\n');
