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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/**
 * Services Page - Technical Indicator Services
 */

class ServicesPage {
  constructor() {
    this.services = [];
    this.currentCategory = 'all';
    this.currentSymbol = 'BTC';
    this.currentTimeframe = '1h';
  }

  async init() {
    console.log('[Services] Initializing...');
    
    this.bindEvents();
    await this.loadServices();
    this.checkUrlParams();
    
    console.log('[Services] Ready');
  }

  bindEvents() {
    // Refresh button
    document.getElementById('refresh-btn')?.addEventListener('click', () => {
      this.loadServices();
    });

    // Symbol input
    document.getElementById('symbol-input')?.addEventListener('change', (e) => {
      this.currentSymbol = e.target.value.toUpperCase() || 'BTC';
    });

    // Timeframe select
    document.getElementById('timeframe-select')?.addEventListener('change', (e) => {
      this.currentTimeframe = e.target.value || '1h';
    });

    // Analyze all button
    document.getElementById('analyze-all-btn')?.addEventListener('click', () => {
      this.analyzeAll();
    });

    // Category buttons
    document.querySelectorAll('.category-btn').forEach(btn => {
      btn.addEventListener('click', (e) => {
        document.querySelectorAll('.category-btn').forEach(b => b.classList.remove('active'));
        e.target.classList.add('active');
        this.currentCategory = e.target.dataset.category;
        this.filterServices();
      });
    });
  }

  checkUrlParams() {
    const params = new URLSearchParams(window.location.search);
    const service = params.get('service');
    
    if (service) {
      // Auto-analyze the specific service
      setTimeout(() => {
        this.analyzeService(service);
      }, 500);
    }
  }

  async loadServices() {
    const grid = document.getElementById('services-grid');
    if (!grid) return;

    grid.innerHTML = `
      <div class="loading-state">
        <div class="loading-spinner"></div>
        <p>Loading indicator services...</p>
      </div>
    `;

    try {
      const response = await fetch('/api/indicators/services');
      
      if (response.ok) {
        const data = await response.json();
        this.services = data.services || [];
        console.log('[Services] Loaded', this.services.length, 'services');
      } else {
        // Use fallback data
        this.services = this.getFallbackServices();
      }
    } catch (error) {
      console.error('[Services] Load error:', error);
      this.services = this.getFallbackServices();
    }

    this.renderServices();
    this.updateTimestamp();
  }

  getFallbackServices() {
    return [
      {
        id: 'bollinger_bands',
        name: 'Bollinger Bands',
        description: 'Volatility bands placed above and below a moving average. Identifies overbought/oversold conditions and potential breakouts.',
        endpoint: '/api/indicators/bollinger-bands',
        parameters: ['symbol', 'timeframe', 'period', 'std_dev'],
        icon: 'πŸ“Š',
        category: 'volatility'
      },
      {
        id: 'stoch_rsi',
        name: 'Stochastic RSI',
        description: 'Combines Stochastic oscillator with RSI for enhanced momentum detection. Great for identifying extreme conditions.',
        endpoint: '/api/indicators/stoch-rsi',
        parameters: ['symbol', 'timeframe', 'rsi_period', 'stoch_period'],
        icon: 'πŸ“ˆ',
        category: 'momentum'
      },
      {
        id: 'atr',
        name: 'Average True Range (ATR)',
        description: 'Measures market volatility by analyzing the range of price movements. Useful for setting stop losses.',
        endpoint: '/api/indicators/atr',
        parameters: ['symbol', 'timeframe', 'period'],
        icon: 'πŸ“‰',
        category: 'volatility'
      },
      {
        id: 'sma',
        name: 'Simple Moving Average (SMA)',
        description: 'Average price over specified periods (20, 50, 200). Identifies trend direction and support/resistance levels.',
        endpoint: '/api/indicators/sma',
        parameters: ['symbol', 'timeframe'],
        icon: '〰️',
        category: 'trend'
      },
      {
        id: 'ema',
        name: 'Exponential Moving Average (EMA)',
        description: 'Weighted moving average giving more weight to recent prices. More responsive to current price action.',
        endpoint: '/api/indicators/ema',
        parameters: ['symbol', 'timeframe'],
        icon: 'πŸ“',
        category: 'trend'
      },
      {
        id: 'macd',
        name: 'MACD',
        description: 'Moving Average Convergence Divergence. Trend-following momentum indicator showing relationship between EMAs.',
        endpoint: '/api/indicators/macd',
        parameters: ['symbol', 'timeframe', 'fast', 'slow', 'signal'],
        icon: 'πŸ”€',
        category: 'momentum'
      },
      {
        id: 'rsi',
        name: 'RSI',
        description: 'Relative Strength Index. Momentum oscillator measuring speed and magnitude of price movements (0-100).',
        endpoint: '/api/indicators/rsi',
        parameters: ['symbol', 'timeframe', 'period'],
        icon: 'πŸ’ͺ',
        category: 'momentum'
      },
      {
        id: 'comprehensive',
        name: 'Comprehensive Analysis',
        description: 'All indicators combined with trading signals. Get a complete market overview with actionable recommendations.',
        endpoint: '/api/indicators/comprehensive',
        parameters: ['symbol', 'timeframe'],
        icon: '🎯',
        category: 'analysis'
      }
    ];
  }

  filterServices() {
    this.renderServices();
  }

  renderServices() {
    const grid = document.getElementById('services-grid');
    if (!grid) return;

    const filteredServices = this.currentCategory === 'all'
      ? this.services
      : this.services.filter(s => s.category === this.currentCategory);

    if (filteredServices.length === 0) {
      grid.innerHTML = `
        <div class="error-state">
          <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10"></circle>
            <line x1="12" y1="8" x2="12" y2="12"></line>
            <line x1="12" y1="16" x2="12.01" y2="16"></line>
          </svg>
          <h3>No services found</h3>
          <p>No indicator services match the selected category.</p>
        </div>
      `;
      return;
    }

    grid.innerHTML = filteredServices.map(service => `
      <div class="service-card-large" data-service="${service.id}">
        <div class="service-card-header">
          <div class="service-card-icon">${service.icon}</div>
          <div class="service-card-title">
            <h3>${service.name}</h3>
            <span class="category-tag">${service.category}</span>
          </div>
        </div>
        <div class="service-card-body">
          <p class="service-card-desc">${service.description}</p>
          <div class="service-card-params">
            ${service.parameters.map(p => `<span class="param-tag">${p}</span>`).join('')}
          </div>
        </div>
        <div class="service-card-footer">
          <div class="service-status">
            <span class="status-dot"></span>
            <span>Available</span>
          </div>
          <button class="btn btn-primary" onclick="servicesPage.analyzeService('${service.id}')">
            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
            </svg>
            Analyze
          </button>
        </div>
      </div>
    `).join('');
  }

  async analyzeService(serviceId) {
    const resultsSection = document.getElementById('results-section');
    const resultsContainer = document.getElementById('results-container');
    
    if (!resultsSection || !resultsContainer) return;

    // Get current values
    const symbolInput = document.getElementById('symbol-input');
    const timeframeSelect = document.getElementById('timeframe-select');
    
    this.currentSymbol = symbolInput?.value?.toUpperCase() || 'BTC';
    this.currentTimeframe = timeframeSelect?.value || '1h';

    // Show results section
    resultsSection.style.display = 'block';
    resultsContainer.innerHTML = `
      <div class="loading-state">
        <div class="loading-spinner"></div>
        <p>Analyzing ${this.currentSymbol} with ${serviceId}...</p>
      </div>
    `;

    // Scroll to results
    resultsSection.scrollIntoView({ behavior: 'smooth' });

    try {
      const service = this.services.find(s => s.id === serviceId);
      if (!service) throw new Error('Service not found');

      const url = `${service.endpoint}?symbol=${encodeURIComponent(this.currentSymbol)}&timeframe=${encodeURIComponent(this.currentTimeframe)}`;
      const response = await fetch(url);

      if (!response.ok) {
        throw new Error(`HTTP ${response.status}`);
      }

      const result = await response.json();
      this.renderResult(service, result);
    } catch (error) {
      console.error('[Services] Analysis error:', error);
      resultsContainer.innerHTML = `
        <div class="error-state">
          <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10"></circle>
            <line x1="15" y1="9" x2="9" y2="15"></line>
            <line x1="9" y1="9" x2="15" y2="15"></line>
          </svg>
          <h3>Analysis Failed</h3>
          <p>${error.message}</p>
          <button class="btn btn-primary" onclick="servicesPage.analyzeService('${serviceId}')">Retry</button>
        </div>
      `;
    }
  }

  async analyzeAll() {
    const resultsSection = document.getElementById('results-section');
    const resultsContainer = document.getElementById('results-container');
    
    if (!resultsSection || !resultsContainer) return;

    // Get current values
    const symbolInput = document.getElementById('symbol-input');
    const timeframeSelect = document.getElementById('timeframe-select');
    
    this.currentSymbol = symbolInput?.value?.toUpperCase() || 'BTC';
    this.currentTimeframe = timeframeSelect?.value || '1h';

    // Show loading
    resultsSection.style.display = 'block';
    resultsContainer.innerHTML = `
      <div class="loading-state">
        <div class="loading-spinner"></div>
        <p>Running comprehensive analysis on ${this.currentSymbol}...</p>
      </div>
    `;

    resultsSection.scrollIntoView({ behavior: 'smooth' });

    try {
      const url = `/api/indicators/comprehensive?symbol=${encodeURIComponent(this.currentSymbol)}&timeframe=${encodeURIComponent(this.currentTimeframe)}`;
      const response = await fetch(url, {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
        },
      });

      // Handle different response scenarios
      let result;
      const contentType = response.headers.get('content-type');
      
      if (contentType && contentType.includes('application/json')) {
        result = await response.json();
      } else {
        throw new Error(`Unexpected response type: ${contentType || 'unknown'}`);
      }

      // Check if the result indicates an error even with 200 status
      if (result.success === false && result.error) {
        console.warn('[Services] API returned error in response:', result.error);
        this.showToast(`⚠️ ${result.error}`, 'warning');
      }

      // Render even with warnings/errors, as fallback data is still useful
      this.renderComprehensiveResult(result);
      
      // Show warning if using fallback data
      if (result.source === 'fallback' || result.warning) {
        this.showToast('⚠️ Using fallback data - some services may be unavailable', 'warning');
      }
      
    } catch (error) {
      console.error('[Services] Comprehensive analysis error:', error);
      
      // More detailed error message
      let errorMessage = 'Unable to complete analysis';
      if (error.message.includes('HTTP 500')) {
        errorMessage = 'Server error - the analysis service is temporarily unavailable';
      } else if (error.message.includes('Failed to fetch')) {
        errorMessage = 'Network error - please check your connection';
      } else if (error.message.includes('timeout')) {
        errorMessage = 'Request timeout - the service took too long to respond';
      } else {
        errorMessage = error.message;
      }
      
      resultsContainer.innerHTML = `
        <div class="error-state">
          <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10"></circle>
            <line x1="15" y1="9" x2="9" y2="15"></line>
            <line x1="9" y1="9" x2="15" y2="15"></line>
          </svg>
          <h3>Analysis Failed</h3>
          <p style="margin: 1rem 0;">${errorMessage}</p>
          <div style="display: flex; gap: 1rem; justify-content: center; flex-wrap: wrap;">
            <button class="btn btn-primary" onclick="servicesPage.analyzeAll()">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="23 4 23 10 17 10"></polyline>
                <path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path>
              </svg>
              Retry
            </button>
            <button class="btn btn-secondary" onclick="window.location.href='/static/pages/service-health/index.html'">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
              </svg>
              Check Service Status
            </button>
          </div>
        </div>
      `;
      
      this.showToast(`❌ ${errorMessage}`, 'error');
    }
  }

  renderResult(service, result) {
    const resultsContainer = document.getElementById('results-container');
    if (!resultsContainer) return;

    const signalClass = this.getSignalClass(result.signal);
    const data = result.data || {};

    let valuesHtml = '';
    for (const [key, value] of Object.entries(data)) {
      if (value !== null && value !== undefined) {
        valuesHtml += `
          <div class="result-value">
            <span class="label">${this.formatLabel(key)}</span>
            <span class="value">${this.formatValue(value)}</span>
          </div>
        `;
      }
    }

    resultsContainer.innerHTML = `
      <div class="result-card">
        <div class="result-card-header">
          <h4>
            <span class="indicator-icon">${service.icon}</span>
            ${service.name}
          </h4>
          <span class="signal-badge ${signalClass}">${result.signal || 'N/A'}</span>
        </div>
        <div class="result-card-body">
          <div class="result-values">
            ${valuesHtml}
          </div>
          <div class="result-description">
            <p>${result.description || 'No description available'}</p>
          </div>
        </div>
      </div>
    `;
  }

  renderComprehensiveResult(result) {
    const resultsContainer = document.getElementById('results-container');
    if (!resultsContainer) return;

    const indicators = result.indicators || {};
    const signals = result.signals || {};

    let cardsHtml = '';

    // Overall signal card
    const overallClass = this.getSignalClass(result.overall_signal?.toLowerCase());
    cardsHtml += `
      <div class="result-card" style="grid-column: 1 / -1;">
        <div class="result-card-header" style="background: linear-gradient(135deg, rgba(20, 184, 166, 0.2), rgba(6, 182, 212, 0.15));">
          <h4>
            <span class="indicator-icon">🎯</span>
            Overall Analysis - ${result.symbol || this.currentSymbol}
          </h4>
          <span class="signal-badge ${overallClass}">${result.overall_signal || 'N/A'}</span>
        </div>
        <div class="result-card-body">
          <div class="result-values">
            <div class="result-value">
              <span class="label">Current Price</span>
              <span class="value">${this.formatValue(result.current_price)}</span>
            </div>
            <div class="result-value">
              <span class="label">Confidence</span>
              <span class="value">${result.confidence || 0}%</span>
            </div>
          </div>
          <div class="result-description">
            <p><strong>Recommendation:</strong> ${result.recommendation || 'No recommendation available'}</p>
          </div>
        </div>
      </div>
    `;

    // Individual indicator cards
    const indicatorMeta = {
      bollinger_bands: { icon: 'πŸ“Š', name: 'Bollinger Bands' },
      stoch_rsi: { icon: 'πŸ“ˆ', name: 'Stochastic RSI' },
      atr: { icon: 'πŸ“‰', name: 'ATR' },
      sma: { icon: '〰️', name: 'SMA' },
      ema: { icon: 'πŸ“', name: 'EMA' },
      macd: { icon: 'πŸ”€', name: 'MACD' },
      rsi: { icon: 'πŸ’ͺ', name: 'RSI' }
    };

    for (const [key, data] of Object.entries(indicators)) {
      const meta = indicatorMeta[key] || { icon: 'πŸ“Š', name: key };
      const signal = signals[key] || 'neutral';
      const signalClass = this.getSignalClass(signal);

      let valuesHtml = '';
      if (typeof data === 'object') {
        for (const [k, v] of Object.entries(data)) {
          if (v !== null && v !== undefined) {
            valuesHtml += `
              <div class="result-value">
                <span class="label">${this.formatLabel(k)}</span>
                <span class="value">${this.formatValue(v)}</span>
              </div>
            `;
          }
        }
      }

      cardsHtml += `
        <div class="result-card">
          <div class="result-card-header">
            <h4>
              <span class="indicator-icon">${meta.icon}</span>
              ${meta.name}
            </h4>
            <span class="signal-badge ${signalClass}">${signal}</span>
          </div>
          <div class="result-card-body">
            <div class="result-values">
              ${valuesHtml || '<p style="grid-column: 1/-1; text-align: center; color: var(--text-muted);">No data</p>'}
            </div>
          </div>
        </div>
      `;
    }

    resultsContainer.innerHTML = cardsHtml;
  }

  getSignalClass(signal) {
    if (!signal) return 'neutral';
    const s = signal.toLowerCase();
    
    if (s.includes('buy') || s.includes('bullish') || s.includes('oversold') || s.includes('strong_buy')) {
      return 'bullish';
    }
    if (s.includes('sell') || s.includes('bearish') || s.includes('overbought') || s.includes('strong_sell')) {
      return 'bearish';
    }
    return 'neutral';
  }

  formatLabel(key) {
    return key
      .replace(/_/g, ' ')
      .replace(/([A-Z])/g, ' $1')
      .split(' ')
      .map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
      .join(' ');
  }

  formatValue(value) {
    if (value === null || value === undefined) return 'β€”';
    if (typeof value === 'number') {
      if (value > 1000000) return (value / 1000000).toFixed(2) + 'M';
      if (value > 1000) return (value / 1000).toFixed(2) + 'K';
      if (value < 0.0001 && value > 0) return value.toExponential(2);
      if (Number.isInteger(value)) return value.toLocaleString();
      return value.toFixed(value < 1 ? 4 : 2);
    }
    return String(value);
  }

  updateTimestamp() {
    const el = document.getElementById('last-update');
    if (el) {
      el.textContent = `Updated: ${new Date().toLocaleTimeString()}`;
    }
  }

  showToast(message, type = 'info') {
    console.log(`[Toast ${type}]`, message);
    // Implement toast if needed
  }
}

// Initialize
const servicesPage = new ServicesPage();
servicesPage.init();

// Expose globally
window.servicesPage = servicesPage;

export default servicesPage;