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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f5e454
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import { Component, OnInit, OnDestroy, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, ReactiveFormsModule, Validators, FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatCardModule } from '@angular/material/card';
import { MatSelectModule } from '@angular/material/select';
import { MatDividerModule } from '@angular/material/divider';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { Subject, takeUntil } from 'rxjs';

import { ApiService } from '../../services/api.service';
import { EnvironmentService } from '../../services/environment.service';
import { Router } from '@angular/router';

interface ChatMessage {
  author: 'user' | 'assistant';
  text: string;
  timestamp?: Date;
  audioUrl?: string;
}

@Component({
  selector: 'app-chat',
  standalone: true,
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    MatButtonModule,
    MatIconModule,
    MatFormFieldModule,
    MatInputModule,
    MatCardModule,
    MatSelectModule,
    MatDividerModule,
    MatTooltipModule,
    MatProgressSpinnerModule,
    MatCheckboxModule,
    MatDialogModule,
    MatSnackBarModule
  ],
  templateUrl: './chat.component.html',
  styleUrls: ['./chat.component.scss']
})
export class ChatComponent implements OnInit, OnDestroy, AfterViewChecked {
  @ViewChild('scrollMe') private myScrollContainer!: ElementRef;
  @ViewChild('audioPlayer') private audioPlayer!: ElementRef<HTMLAudioElement>;
  @ViewChild('waveformCanvas') private waveformCanvas!: ElementRef<HTMLCanvasElement>;

  projects: string[] = [];
  selectedProject: string | null = null;
  useTTS = false;
  ttsAvailable = false;
  selectedLocale: string = 'tr';
  availableLocales: any[] = [];

  sessionId: string | null = null;
  messages: ChatMessage[] = [];
  input = this.fb.control('', Validators.required);
  
  loading = false;
  error = '';
  playingAudio = false;
  useSTT = false;
  sttAvailable = false;
  isListening = false;
  
  // Audio visualization
  audioContext?: AudioContext;
  analyser?: AnalyserNode;
  animationId?: number;
  
  private destroyed$ = new Subject<void>();
  private shouldScroll = false;

  constructor(

    private fb: FormBuilder, 

    private api: ApiService,

    private environmentService: EnvironmentService,

    private dialog: MatDialog,

    private router: Router,

    private snackBar: MatSnackBar

  ) {}

  ngOnInit(): void {
    this.loadProjects();
    this.loadAvailableLocales();
    this.checkTTSAvailability();
    this.checkSTTAvailability();
    
    // Initialize Audio Context with error handling
    try {
      this.audioContext = new (window.AudioContext || (window as any).webkitAudioContext)();
    } catch (error) {
      console.error('Failed to create AudioContext:', error);
    }
    
    // Watch for STT toggle changes
    this.watchSTTToggle();
  }

  loadAvailableLocales(): void {
    this.api.getAvailableLocales().pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: (response) => {
        this.availableLocales = response.locales;
        this.selectedLocale = response.default || 'tr';
      },
      error: (err) => {
        console.error('Failed to load locales:', err);
        // Fallback locales
        this.availableLocales = [
          { code: 'tr', name: 'Türkçe' },
          { code: 'en', name: 'English' }
        ];
      }
    });
  }  

  private watchSTTToggle(): void {
    // When STT is toggled, provide feedback
    // This could be implemented with form control valueChanges if needed
  }
  
  ngAfterViewChecked() {
    if (this.shouldScroll) {
      this.scrollToBottom();
      this.shouldScroll = false;
    }
  }

  ngOnDestroy(): void {
    this.destroyed$.next();
    this.destroyed$.complete();
    
    // Cleanup audio resources
    this.cleanupAudio();
  }

  private cleanupAudio(): void {
    if (this.animationId) {
      cancelAnimationFrame(this.animationId);
      this.animationId = undefined;
    }
    
    if (this.audioContext && this.audioContext.state !== 'closed') {
      this.audioContext.close().catch(err => console.error('Failed to close audio context:', err));
    }
    
    // Clean up audio URLs
    this.messages.forEach(msg => {
      if (msg.audioUrl) {
        URL.revokeObjectURL(msg.audioUrl);
      }
    });
  }

  private checkSTTAvailability(): void {
    this.api.getEnvironment().pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: (env) => {
        this.sttAvailable = env.stt_provider?.name !== 'no_stt';
        if (!this.sttAvailable) {
          this.useSTT = false;
        }
      },
      error: (err) => {
        console.error('Failed to check STT availability:', err);
        this.sttAvailable = false;
      }
    });
  }
  
  async startRealtimeChat(): Promise<void> {
    if (!this.selectedProject) {
      this.error = 'Please select a project first';
      this.snackBar.open(this.error, 'Close', { duration: 3000 });
      return;
    }
    
    if (!this.sttAvailable || !this.useSTT) {
      this.error = 'STT must be enabled for real-time chat';
      this.snackBar.open(this.error, 'Close', { duration: 5000 });
      return;
    }
    
    this.loading = true;
    this.error = '';
    
    this.api.startChat(this.selectedProject, true, this.selectedLocale).pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: res => {
        // Store session ID for realtime component
        localStorage.setItem('current_session_id', res.session_id);
        localStorage.setItem('current_project', this.selectedProject || '');
        localStorage.setItem('current_locale', this.selectedLocale);
        localStorage.setItem('use_tts', this.useTTS.toString());
        
        // Open realtime chat dialog
        this.openRealtimeDialog(res.session_id);
        
        this.loading = false;
      },
      error: (err) => {
        this.error = this.getErrorMessage(err);
        this.loading = false;
        this.snackBar.open(this.error, 'Close', { 
          duration: 5000,
          panelClass: 'error-snackbar'
        });
      }
    });
  }
  
  private async openRealtimeDialog(sessionId: string): Promise<void> {
    try {
      const { RealtimeChatComponent } = await import('./realtime-chat.component');
      
      const dialogRef = this.dialog.open(RealtimeChatComponent, {
        width: '90%',
        maxWidth: '900px',
        height: '85vh',
        maxHeight: '800px',
        disableClose: false,
        panelClass: 'realtime-chat-dialog',
        data: { 
          sessionId: sessionId,
          projectName: this.selectedProject 
        }
      });
      
      dialogRef.afterClosed().pipe(
        takeUntil(this.destroyed$)
      ).subscribe(result => {
        // Clean up session data
        localStorage.removeItem('current_session_id');
        localStorage.removeItem('current_project');
        localStorage.removeItem('current_locale');
        localStorage.removeItem('use_tts');
        
        // If session was active, end it
        if (result === 'session_active' && sessionId) {
          this.api.endSession(sessionId).pipe(
            takeUntil(this.destroyed$)
          ).subscribe({
            next: () => console.log('Session ended'),
            error: (err: any) => console.error('Failed to end session:', err)
          });
        }
      });
    } catch (error) {
      console.error('Failed to load realtime chat:', error);
      this.snackBar.open('Failed to open realtime chat', 'Close', {
        duration: 3000,
        panelClass: 'error-snackbar'
      });
    }
  }
  
  loadProjects(): void {
    this.loading = true;
    this.error = '';
    
    this.api.getChatProjects().pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: projects => {
        this.projects = projects;
        this.loading = false;
        if (projects.length === 0) {
          this.error = 'No enabled projects found. Please enable a project with published version.';
        }
      },
      error: (err) => {
        this.error = 'Failed to load projects';
        this.loading = false;
        this.snackBar.open(this.error, 'Close', {
          duration: 5000,
          panelClass: 'error-snackbar'
        });
      }
    });
  }

  checkTTSAvailability(): void {
    // Subscribe to environment updates
    this.environmentService.environment$.pipe(
      takeUntil(this.destroyed$)
    ).subscribe(env => {
      if (env) {
        this.ttsAvailable = env.tts_provider?.name !== 'no_tts';
        if (!this.ttsAvailable) {
          this.useTTS = false;
        }
      }
    });
    
    // Get current environment
    this.api.getEnvironment().pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: (env) => {
        this.ttsAvailable = env.tts_provider?.name !== 'no_tts';
        if (!this.ttsAvailable) {
          this.useTTS = false;
        }
      }
    });
  }
  
  startChat(): void {
    if (!this.selectedProject) {
      this.snackBar.open('Please select a project', 'Close', { duration: 3000 });
      return;
    }
    
    if (this.useSTT) {
      this.snackBar.open('For voice input, please use Real-time Chat', 'Close', { duration: 3000 });
      return;
    }
    
    this.loading = true;
    this.error = '';
    
    this.api.startChat(this.selectedProject, false, this.selectedLocale).pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: res => {
        this.sessionId = res.session_id;
        const message: ChatMessage = { 
          author: 'assistant', 
          text: res.answer,
          timestamp: new Date()
        };
        
        this.messages = [message];
        this.loading = false;
        this.shouldScroll = true;
        
        // Generate TTS if enabled
        if (this.useTTS && this.ttsAvailable) {
          this.generateTTS(res.answer, this.messages.length - 1);
        }
      },
      error: (err) => {
        this.error = this.getErrorMessage(err);
        this.loading = false;
        this.snackBar.open(this.error, 'Close', {
          duration: 5000,
          panelClass: 'error-snackbar'
        });
      }
    });
  }

  send(): void {
    if (!this.sessionId || this.input.invalid || this.loading) return;
    
    const text = this.input.value!.trim();
    if (!text) return;

    // Add user message
    this.messages.push({ 
      author: 'user', 
      text,
      timestamp: new Date()
    });
    
    this.input.reset();
    this.loading = true;
    this.shouldScroll = true;

    // Send to backend
    this.api.chat(this.sessionId, text).pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: res => {
        const message: ChatMessage = { 
          author: 'assistant', 
          text: res.response,
          timestamp: new Date()
        };
        
        this.messages.push(message);
        this.loading = false;
        this.shouldScroll = true;
        
        // Generate TTS if enabled
        if (this.useTTS && this.ttsAvailable) {
          this.generateTTS(res.response, this.messages.length - 1);
        }
      },
      error: (err) => {
        const errorMsg = this.getErrorMessage(err);
        this.messages.push({
          author: 'assistant',
          text: '⚠️ ' + errorMsg,
          timestamp: new Date()
        });
        this.loading = false;
        this.shouldScroll = true;
      }
    });
  }

  generateTTS(text: string, messageIndex: number): void {
    if (!this.ttsAvailable || messageIndex < 0 || messageIndex >= this.messages.length) return;
    
    this.api.generateTTS(text).pipe(
      takeUntil(this.destroyed$)
    ).subscribe({
      next: (audioBlob) => {
        const audioUrl = URL.createObjectURL(audioBlob);
        
        // Clean up old audio URL if exists
        if (this.messages[messageIndex].audioUrl) {
          URL.revokeObjectURL(this.messages[messageIndex].audioUrl!);
        }
        
        this.messages[messageIndex].audioUrl = audioUrl;
        
        // Auto-play the latest message
        if (messageIndex === this.messages.length - 1) {
          setTimeout(() => this.playAudio(audioUrl), 100);
        }
      },
      error: (err) => {
        console.error('TTS generation error:', err);
        this.snackBar.open('Failed to generate audio', 'Close', {
          duration: 3000,
          panelClass: 'error-snackbar'
        });
      }
    });
  }

  playAudio(audioUrl: string): void {
    if (!this.audioPlayer || !audioUrl) return;
    
    const audio = this.audioPlayer.nativeElement;
    
    // Stop current audio if playing
    if (!audio.paused) {
      audio.pause();
      audio.currentTime = 0;
    }
    
    audio.src = audioUrl;
    
    // Set up audio visualization
    if (this.audioContext && this.audioContext.state !== 'closed') {
      this.setupAudioVisualization(audio);
    }
    
    audio.play().then(() => {
      this.playingAudio = true;
    }).catch(err => {
      console.error('Audio play error:', err);
      this.snackBar.open('Failed to play audio', 'Close', {
        duration: 3000,
        panelClass: 'error-snackbar'
      });
    });
    
    audio.onended = () => {
      this.playingAudio = false;
      if (this.animationId) {
        cancelAnimationFrame(this.animationId);
        this.animationId = undefined;
        this.clearWaveform();
      }
    };
    
    audio.onerror = () => {
      this.playingAudio = false;
      console.error('Audio playback error');
    };
  }

  setupAudioVisualization(audio: HTMLAudioElement): void {
    if (!this.audioContext || !this.waveformCanvas || this.audioContext.state === 'closed') return;
    
    try {
      // Check if source already exists for this audio element
      if (!(audio as any).audioSource) {
        const source = this.audioContext.createMediaElementSource(audio);
        this.analyser = this.audioContext.createAnalyser();
        this.analyser.fftSize = 256;
        
        // Connect nodes
        source.connect(this.analyser);
        this.analyser.connect(this.audioContext.destination);
        
        // Store reference to prevent recreation
        (audio as any).audioSource = source;
      }
      
      // Start visualization
      this.drawWaveform();
    } catch (error) {
      console.error('Failed to setup audio visualization:', error);
    }
  }

  drawWaveform(): void {
    if (!this.analyser || !this.waveformCanvas) return;
    
    const canvas = this.waveformCanvas.nativeElement;
    const ctx = canvas.getContext('2d');
    if (!ctx) return;
    
    const bufferLength = this.analyser.frequencyBinCount;
    const dataArray = new Uint8Array(bufferLength);
    
    const draw = () => {
      if (!this.playingAudio) {
        this.clearWaveform();
        return;
      }
      
      this.animationId = requestAnimationFrame(draw);
      
      this.analyser!.getByteFrequencyData(dataArray);
      
      ctx.fillStyle = 'rgb(240, 240, 240)';
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      
      const barWidth = (canvas.width / bufferLength) * 2.5;
      let barHeight;
      let x = 0;
      
      for (let i = 0; i < bufferLength; i++) {
        barHeight = (dataArray[i] / 255) * canvas.height * 0.8;
        
        ctx.fillStyle = `rgb(63, 81, 181)`;
        ctx.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
        
        x += barWidth + 1;
      }
    };
    
    draw();
  }

  clearWaveform(): void {
    if (!this.waveformCanvas) return;
    
    const canvas = this.waveformCanvas.nativeElement;
    const ctx = canvas.getContext('2d');
    if (!ctx) return;
    
    ctx.fillStyle = 'rgb(240, 240, 240)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
  }

  endSession(): void {
    // Clean up current session
    if (this.sessionId) {
      this.api.endSession(this.sessionId).pipe(
        takeUntil(this.destroyed$)
      ).subscribe({
        error: (err) => console.error('Failed to end session:', err)
      });
    }
    
    // Clean up audio URLs
    this.messages.forEach(msg => {
      if (msg.audioUrl) {
        URL.revokeObjectURL(msg.audioUrl);
      }
    });
    
    // Reset state
    this.sessionId = null;
    this.messages = [];
    this.selectedProject = null;
    this.input.reset();
    this.error = '';
    
    // Clean up audio
    if (this.audioPlayer) {
      this.audioPlayer.nativeElement.pause();
      this.audioPlayer.nativeElement.src = '';
    }
    
    if (this.animationId) {
      cancelAnimationFrame(this.animationId);
      this.animationId = undefined;
    }
    
    this.clearWaveform();
  }

  private scrollToBottom(): void {
    try {
      if (this.myScrollContainer?.nativeElement) {
        const element = this.myScrollContainer.nativeElement;
        element.scrollTop = element.scrollHeight;
      }
    } catch(err) {
      console.error('Scroll error:', err);
    }
  }

  private getErrorMessage(error: any): string {
    if (error.status === 0) {
      return 'Unable to connect to server. Please check your connection.';
    } else if (error.status === 401) {
      return 'Session expired. Please login again.';
    } else if (error.status === 403) {
      return 'You do not have permission to use this feature.';
    } else if (error.status === 404) {
      return 'Project or session not found. Please try again.';
    } else if (error.error?.detail) {
      return error.error.detail;
    } else if (error.message) {
      return error.message;
    }
    return 'An unexpected error occurred. Please try again.';
  }
}