import apiClient from './apiClient.js'; class AIAdvisorView { constructor(section) { this.section = section; this.queryForm = section?.querySelector('[data-query-form]'); this.sentimentForm = section?.querySelector('[data-sentiment-form]'); this.queryOutput = section?.querySelector('[data-query-output]'); this.sentimentOutput = section?.querySelector('[data-sentiment-output]'); } init() { if (this.queryForm) { this.queryForm.addEventListener('submit', async (event) => { event.preventDefault(); const formData = new FormData(this.queryForm); await this.handleQuery(formData); }); } if (this.sentimentForm) { this.sentimentForm.addEventListener('submit', async (event) => { event.preventDefault(); const formData = new FormData(this.sentimentForm); await this.handleSentiment(formData); }); } } async handleQuery(formData) { const query = formData.get('query') || ''; if (!query.trim()) return; if (this.queryOutput) { this.queryOutput.innerHTML = '

Processing query...

'; } const result = await apiClient.runQuery({ query }); if (!result.ok) { if (this.queryOutput) { this.queryOutput.innerHTML = `
${result.error}
`; } return; } // Backend returns {success: true, type: ..., message: ..., data: ...} const data = result.data || {}; if (this.queryOutput) { this.queryOutput.innerHTML = `

AI Response

Type: ${data.type || 'general'}

${data.message || 'Query processed'}

${data.data ? `
${JSON.stringify(data.data, null, 2)}
` : ''}
`; } } async handleSentiment(formData) { const text = formData.get('text') || ''; if (!text.trim()) return; if (this.sentimentOutput) { this.sentimentOutput.innerHTML = '

Analyzing sentiment...

'; } const result = await apiClient.analyzeSentiment({ text }); if (!result.ok) { if (this.sentimentOutput) { this.sentimentOutput.innerHTML = `
${result.error}
`; } return; } // Backend returns {success: true, sentiment: ..., confidence: ..., details: ...} const data = result.data || {}; const sentiment = data.sentiment || 'neutral'; const confidence = data.confidence || 0; if (this.sentimentOutput) { this.sentimentOutput.innerHTML = `

Sentiment Analysis

Label: ${sentiment}

Confidence: ${(confidence * 100).toFixed(1)}%

${data.details ? `
${JSON.stringify(data.details, null, 2)}
` : ''}
`; } } } export default AIAdvisorView;