// Debug DeepSeek API key handling
import { default as IAEngine } from './src/DRS/services/IAEngine.js';

class DebugIAEngine extends IAEngine {
    async _callProvider(provider, prompt, options) {
        console.log('🔍 _callProvider called with provider:', provider);
        console.log('🔍 Available API keys:', Object.keys(this.apiKeys || {}));
        console.log('🔍 Mock mode?', this.apiKeys?.mock);

        if (this.apiKeys?.mock) {
            console.log('⚠️ Using mock mode, calling super._generateMockValidation');
            return this._generateMockValidation(prompt, options);
        }

        const apiKey = this.apiKeys?.[provider.toUpperCase() + '_API_KEY'];
        console.log('🔍 API key for', provider, 'exists:', !!apiKey);
        console.log('🔍 API key preview:', apiKey?.substring(0, 15) + '...');

        if (!apiKey) {
            console.log('❌ No API key found for', provider, '- falling back to mock');
            return this._generateMockValidation(prompt, options);
        }

        return super._callProvider(provider, prompt, options);
    }
}

async function debugDeepSeek() {
    console.log('🔧 Debugging IAEngine API key handling...');
    const debugEngine = new DebugIAEngine();
    await new Promise(resolve => setTimeout(resolve, 1000));

    try {
        const result = await debugEngine.validateEducationalContent('Test DeepSeek', {
            preferredProvider: 'deepseek',
            language: 'en'
        });

        console.log('Result provider:', result.provider);
        console.log('Mock generated:', result.mockGenerated);
    } catch (error) {
        console.log('❌ Debug failed:', error.message);
    }
}

debugDeepSeek().catch(console.error);