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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960f82f
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
import { Component, Inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
import { MatSelectModule } from '@angular/material/select';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatChipsModule } from '@angular/material/chips';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatDividerModule } from '@angular/material/divider';
import { MatListModule } from '@angular/material/list';
import { FormsModule } from '@angular/forms';
import { Version } from '../../services/api.service';

interface Difference {
  field: string;
  label: string;
  v1Value: any;
  v2Value: any;
  type: 'added' | 'removed' | 'modified' | 'unchanged';
}

@Component({
  selector: 'app-version-compare-dialog',
  standalone: true,
  imports: [
    CommonModule,
    FormsModule,
    MatDialogModule,
    MatSelectModule,
    MatButtonModule,
    MatIconModule,
    MatChipsModule,
    MatExpansionModule,
    MatDividerModule,
    MatListModule
  ],
  template: `

    <h2 mat-dialog-title>Compare Versions</h2>



    <mat-dialog-content>

      <div class="compare-container">

        <!-- Version Selectors -->

        <div class="version-selectors">

          <mat-form-field appearance="outline">

            <mat-label>Version 1</mat-label>

            <mat-select [(value)]="version1" (selectionChange)="compareVersions()">

              <mat-option *ngFor="let v of versions" [value]="v">

                Version {{ v.no }} - {{ v.caption }}

                <span class="published-marker" *ngIf="v.published">(Published)</span>

              </mat-option>

            </mat-select>

          </mat-form-field>



          <mat-icon class="compare-icon">compare_arrows</mat-icon>



          <mat-form-field appearance="outline">

            <mat-label>Version 2</mat-label>

            <mat-select [(value)]="version2" (selectionChange)="compareVersions()">

              <mat-option *ngFor="let v of versions" [value]="v">

                Version {{ v.no }} - {{ v.caption }}

                <span class="published-marker" *ngIf="v.published">(Published)</span>

              </mat-option>

            </mat-select>

          </mat-form-field>

        </div>



        <!-- Comparison Results -->

        <div class="comparison-results" *ngIf="differences.length > 0">

          

          <!-- Summary -->

          <div class="summary-chips">

            <mat-chip-listbox>

              <mat-chip-option selected>

                <mat-icon>add_circle</mat-icon>

                {{ addedCount }} Added

              </mat-chip-option>

              <mat-chip-option selected color="warn">

                <mat-icon>remove_circle</mat-icon>

                {{ removedCount }} Removed

              </mat-chip-option>

              <mat-chip-option selected color="accent">

                <mat-icon>edit</mat-icon>

                {{ modifiedCount }} Modified

              </mat-chip-option>

            </mat-chip-listbox>

          </div>



          <!-- General Differences -->

          <mat-expansion-panel [expanded]="hasGeneralDifferences">

            <mat-expansion-panel-header>

              <mat-panel-title>

                General Configuration

              </mat-panel-title>

              <mat-panel-description>

                {{ generalDifferences.length }} differences

              </mat-panel-description>

            </mat-expansion-panel-header>



            <mat-list>

              <mat-list-item *ngFor="let diff of generalDifferences">

                <mat-icon matListItemIcon [class]="'diff-' + diff.type">

                  {{ getDiffIcon(diff.type) }}

                </mat-icon>

                <div matListItemTitle>{{ diff.label }}</div>

                <div matListItemLine class="diff-values">

                  <span class="old-value" *ngIf="diff.type !== 'added'">{{ formatValue(diff.v1Value) }}</span>

                  <mat-icon *ngIf="diff.type === 'modified'">arrow_forward</mat-icon>

                  <span class="new-value" *ngIf="diff.type !== 'removed'">{{ formatValue(diff.v2Value) }}</span>

                </div>

              </mat-list-item>

            </mat-list>

          </mat-expansion-panel>



          <!-- LLM Differences -->

          <mat-expansion-panel [expanded]="hasLLMDifferences">

            <mat-expansion-panel-header>

              <mat-panel-title>

                LLM Configuration

              </mat-panel-title>

              <mat-panel-description>

                {{ llmDifferences.length }} differences

              </mat-panel-description>

            </mat-expansion-panel-header>



            <mat-list>

              <mat-list-item *ngFor="let diff of llmDifferences">

                <mat-icon matListItemIcon [class]="'diff-' + diff.type">

                  {{ getDiffIcon(diff.type) }}

                </mat-icon>

                <div matListItemTitle>{{ diff.label }}</div>

                <div matListItemLine class="diff-values">

                  <span class="old-value" *ngIf="diff.type !== 'added'">{{ formatValue(diff.v1Value) }}</span>

                  <mat-icon *ngIf="diff.type === 'modified'">arrow_forward</mat-icon>

                  <span class="new-value" *ngIf="diff.type !== 'removed'">{{ formatValue(diff.v2Value) }}</span>

                </div>

              </mat-list-item>

            </mat-list>

          </mat-expansion-panel>



          <!-- Intent Differences -->

          <mat-expansion-panel [expanded]="hasIntentDifferences">

            <mat-expansion-panel-header>

              <mat-panel-title>

                Intents

              </mat-panel-title>

              <mat-panel-description>

                {{ intentDifferences.added.length + intentDifferences.removed.length + intentDifferences.modified.length }} differences

              </mat-panel-description>

            </mat-expansion-panel-header>



            <div class="intents-comparison">

              <!-- Added Intents -->

              <div class="intent-group" *ngIf="intentDifferences.added.length > 0">

                <h4><mat-icon>add_circle</mat-icon> Added Intents</h4>

                <mat-list>

                  <mat-list-item *ngFor="let intent of intentDifferences.added">

                    <mat-icon matListItemIcon class="diff-added">add</mat-icon>

                    <div matListItemTitle>{{ intent.name }}</div>

                    <div matListItemLine>{{ intent.caption || 'No description' }}</div>

                  </mat-list-item>

                </mat-list>

              </div>



              <!-- Removed Intents -->

              <div class="intent-group" *ngIf="intentDifferences.removed.length > 0">

                <h4><mat-icon>remove_circle</mat-icon> Removed Intents</h4>

                <mat-list>

                  <mat-list-item *ngFor="let intent of intentDifferences.removed">

                    <mat-icon matListItemIcon class="diff-removed">remove</mat-icon>

                    <div matListItemTitle>{{ intent.name }}</div>

                    <div matListItemLine>{{ intent.caption || 'No description' }}</div>

                  </mat-list-item>

                </mat-list>

              </div>



              <!-- Modified Intents -->

              <div class="intent-group" *ngIf="intentDifferences.modified.length > 0">

                <h4><mat-icon>edit</mat-icon> Modified Intents</h4>

                <mat-expansion-panel *ngFor="let intent of intentDifferences.modified">

                  <mat-expansion-panel-header>

                    <mat-panel-title>{{ intent.name }}</mat-panel-title>

                    <mat-panel-description>{{ intent.changes.length }} changes</mat-panel-description>

                  </mat-expansion-panel-header>

                  

                  <mat-list>

                    <mat-list-item *ngFor="let change of intent.changes">

                      <mat-icon matListItemIcon [class]="'diff-' + change.type">

                        {{ getDiffIcon(change.type) }}

                      </mat-icon>

                      <div matListItemTitle>{{ change.label }}</div>

                      <div matListItemLine class="diff-values">

                        <span class="old-value" *ngIf="change.type !== 'added'">{{ formatValue(change.v1Value) }}</span>

                        <mat-icon *ngIf="change.type === 'modified'">arrow_forward</mat-icon>

                        <span class="new-value" *ngIf="change.type !== 'removed'">{{ formatValue(change.v2Value) }}</span>

                      </div>

                    </mat-list-item>

                  </mat-list>

                </mat-expansion-panel>

              </div>

            </div>

          </mat-expansion-panel>



        </div>



        <!-- No Selection State -->

        <div class="empty-state" *ngIf="!version1 || !version2">

          <mat-icon>compare</mat-icon>

          <p>Select two versions to compare</p>

        </div>



        <!-- Same Version State -->

        <div class="empty-state" *ngIf="version1 && version2 && version1.no === version2.no">

          <mat-icon>info</mat-icon>

          <p>Please select different versions to compare</p>

        </div>



        <!-- No Differences State -->

        <div class="empty-state" *ngIf="version1 && version2 && version1.no !== version2.no && differences.length === 0">

          <mat-icon>check_circle</mat-icon>

          <p>These versions are identical</p>

        </div>

      </div>

    </mat-dialog-content>



    <mat-dialog-actions align="end">

      <button mat-button (click)="close()">Close</button>

    </mat-dialog-actions>

  `,
  styles: [`

    .compare-container {

      min-width: 800px;

      max-width: 1000px;

    }



    .version-selectors {

      display: flex;

      gap: 24px;

      align-items: center;

      justify-content: center;

      margin-bottom: 32px;



      mat-form-field {

        flex: 1;

        max-width: 350px;

      }



      .compare-icon {

        font-size: 32px;

        width: 32px;

        height: 32px;

        color: #666;

      }



      .published-marker {

        color: #4caf50;

        font-weight: 500;

        margin-left: 8px;

      }

    }



    .summary-chips {

      margin-bottom: 24px;

      display: flex;

      justify-content: center;



      mat-chip {

        margin: 0 4px;



        mat-icon {

          margin-right: 4px;

        }

      }

    }



    .comparison-results {

      mat-expansion-panel {

        margin-bottom: 16px;

      }

    }



    .diff-values {

      display: flex;

      align-items: center;

      gap: 8px;

      margin-top: 4px;



      .old-value {

        color: #d32f2f;

        text-decoration: line-through;

      }



      .new-value {

        color: #388e3c;

        font-weight: 500;

      }



      mat-icon {

        font-size: 16px;

        width: 16px;

        height: 16px;

        color: #666;

      }

    }



    .diff-added {

      color: #388e3c;

    }



    .diff-removed {

      color: #d32f2f;

    }



    .diff-modified {

      color: #1976d2;

    }



    .intents-comparison {

      .intent-group {

        margin-bottom: 24px;



        h4 {

          display: flex;

          align-items: center;

          gap: 8px;

          margin-bottom: 12px;

          color: #666;



          mat-icon {

            font-size: 20px;

            width: 20px;

            height: 20px;

          }

        }



        mat-expansion-panel {

          margin-bottom: 8px;

        }

      }

    }



    .empty-state {

      text-align: center;

      padding: 60px 20px;



      mat-icon {

        font-size: 64px;

        width: 64px;

        height: 64px;

        color: #e0e0e0;

        margin-bottom: 16px;

      }



      p {

        color: #666;

        font-size: 16px;

      }

    }

  `]
})
export default class VersionCompareDialogComponent {
  versions: Version[];
  version1: Version | null = null;
  version2: Version | null = null;
  
  differences: Difference[] = [];
  generalDifferences: Difference[] = [];
  llmDifferences: Difference[] = [];
  intentDifferences = {
    added: [] as any[],
    removed: [] as any[],
    modified: [] as any[]
  };

  constructor(

    public dialogRef: MatDialogRef<VersionCompareDialogComponent>,

    @Inject(MAT_DIALOG_DATA) public data: { versions: Version[], selectedVersion?: Version }

  ) {
    this.versions = data.versions;
    
    // Pre-select versions
    if (data.selectedVersion) {
      this.version1 = data.selectedVersion;
      // Select the next most recent version as version2
      const otherVersions = this.versions.filter(v => v.no !== data.selectedVersion!.no);
      if (otherVersions.length > 0) {
        this.version2 = otherVersions[0];
        this.compareVersions();
      }
    }
  }

  get addedCount(): number {
    return this.differences.filter(d => d.type === 'added').length + this.intentDifferences.added.length;
  }

  get removedCount(): number {
    return this.differences.filter(d => d.type === 'removed').length + this.intentDifferences.removed.length;
  }

  get modifiedCount(): number {
    return this.differences.filter(d => d.type === 'modified').length + this.intentDifferences.modified.length;
  }

  get hasGeneralDifferences(): boolean {
    return this.generalDifferences.length > 0;
  }

  get hasLLMDifferences(): boolean {
    return this.llmDifferences.length > 0;
  }

  get hasIntentDifferences(): boolean {
    return this.intentDifferences.added.length > 0 || 
           this.intentDifferences.removed.length > 0 || 
           this.intentDifferences.modified.length > 0;
  }

  compareVersions() {
    if (!this.version1 || !this.version2 || this.version1.no === this.version2.no) {
      this.differences = [];
      this.generalDifferences = [];
      this.llmDifferences = [];
      this.intentDifferences = { added: [], removed: [], modified: [] };
      return;
    }

    this.differences = [];
    
    // Compare general fields
    this.compareField('caption', 'Caption', this.version1.caption, this.version2.caption);
    this.compareField('general_prompt', 'General Prompt', 
                     (this.version1 as any).general_prompt, 
                     (this.version2 as any).general_prompt);
    this.compareField('published', 'Published Status', this.version1.published, this.version2.published);

    // Compare LLM configuration
    if (this.version1.llm && this.version2.llm) {
      this.compareField('llm.repo_id', 'Model Repository', 
                       this.version1.llm.repo_id, 
                       this.version2.llm.repo_id);
      this.compareField('llm.use_fine_tune', 'Use Fine-Tune', 
                       this.version1.llm.use_fine_tune, 
                       this.version2.llm.use_fine_tune);
      
      if (this.version1.llm.use_fine_tune || this.version2.llm.use_fine_tune) {
        this.compareField('llm.fine_tune_zip', 'Fine-Tune ZIP', 
                         this.version1.llm.fine_tune_zip, 
                         this.version2.llm.fine_tune_zip);
      }

      // Compare generation config
      const gc1 = this.version1.llm.generation_config;
      const gc2 = this.version2.llm.generation_config;
      if (gc1 && gc2) {
        this.compareField('llm.generation_config.max_new_tokens', 'Max Tokens', 
                         gc1.max_new_tokens, gc2.max_new_tokens);
        this.compareField('llm.generation_config.temperature', 'Temperature', 
                         gc1.temperature, gc2.temperature);
        this.compareField('llm.generation_config.top_p', 'Top P', 
                         gc1.top_p, gc2.top_p);
        this.compareField('llm.generation_config.repetition_penalty', 'Repetition Penalty', 
                         gc1.repetition_penalty, gc2.repetition_penalty);
      }
    }

    // Compare intents
    this.compareIntents();

    // Categorize differences
    this.generalDifferences = this.differences.filter(d => 
      !d.field.startsWith('llm.') && !d.field.startsWith('intent.'));
    this.llmDifferences = this.differences.filter(d => d.field.startsWith('llm.'));
  }

  private compareField(field: string, label: string, v1Value: any, v2Value: any) {
    if (v1Value === v2Value) {
      return;
    }

    let type: 'added' | 'removed' | 'modified';
    if (v1Value === undefined || v1Value === null || v1Value === '') {
      type = 'added';
    } else if (v2Value === undefined || v2Value === null || v2Value === '') {
      type = 'removed';
    } else {
      type = 'modified';
    }

    this.differences.push({
      field,
      label,
      v1Value,
      v2Value,
      type
    });
  }

  private compareIntents() {
    const intents1 = this.version1?.intents || [];
    const intents2 = this.version2?.intents || [];

    const intents1Map = new Map(intents1.map(i => [i.name, i]));
    const intents2Map = new Map(intents2.map(i => [i.name, i]));

    // Find added intents
    this.intentDifferences.added = intents2.filter(i => !intents1Map.has(i.name));

    // Find removed intents
    this.intentDifferences.removed = intents1.filter(i => !intents2Map.has(i.name));

    // Find modified intents
    this.intentDifferences.modified = [];
    for (const [name, intent1] of intents1Map) {
      const intent2 = intents2Map.get(name);
      if (intent2) {
        const changes = this.compareIntentDetails(intent1, intent2);
        if (changes.length > 0) {
          this.intentDifferences.modified.push({
            name,
            changes
          });
        }
      }
    }
  }

  private compareIntentDetails(intent1: any, intent2: any): Difference[] {
    const changes: Difference[] = [];

    // Compare basic fields
    if (intent1.caption !== intent2.caption) {
      changes.push({
        field: `intent.${intent1.name}.caption`,
        label: 'Caption',
        v1Value: intent1.caption,
        v2Value: intent2.caption,
        type: 'modified'
      });
    }

    if (intent1.detection_prompt !== intent2.detection_prompt) {
      changes.push({
        field: `intent.${intent1.name}.detection_prompt`,
        label: 'Detection Prompt',
        v1Value: intent1.detection_prompt,
        v2Value: intent2.detection_prompt,
        type: 'modified'
      });
    }

    if (intent1.action !== intent2.action) {
      changes.push({
        field: `intent.${intent1.name}.action`,
        label: 'API Action',
        v1Value: intent1.action,
        v2Value: intent2.action,
        type: 'modified'
      });
    }

    // Compare examples
    const examples1 = intent1.examples || [];
    const examples2 = intent2.examples || [];
    if (JSON.stringify(examples1) !== JSON.stringify(examples2)) {
      changes.push({
        field: `intent.${intent1.name}.examples`,
        label: 'Examples',
        v1Value: `${examples1.length} examples`,
        v2Value: `${examples2.length} examples`,
        type: 'modified'
      });
    }

    // Compare parameters
    const params1 = intent1.parameters || [];
    const params2 = intent2.parameters || [];
    if (JSON.stringify(params1) !== JSON.stringify(params2)) {
      changes.push({
        field: `intent.${intent1.name}.parameters`,
        label: 'Parameters',
        v1Value: `${params1.length} parameters`,
        v2Value: `${params2.length} parameters`,
        type: 'modified'
      });
    }

    return changes;
  }

  getDiffIcon(type: string): string {
    switch (type) {
      case 'added': return 'add_circle';
      case 'removed': return 'remove_circle';
      case 'modified': return 'edit';
      default: return 'circle';
    }
  }

  formatValue(value: any): string {
    if (value === null || value === undefined) return 'Not set';
    if (typeof value === 'boolean') return value ? 'Yes' : 'No';
    if (typeof value === 'string' && value.length > 100) {
      return value.substring(0, 100) + '...';
    }
    return String(value);
  }

  close() {
    this.dialogRef.close();
  }
}