import { Component, inject, signal, OnInit } from '@angular/core'; import { CommonModule, DatePipe } from '@angular/common'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { NzCardModule } from 'ng-zorro-antd/card'; import { NzButtonModule } from 'ng-zorro-antd/button'; import { NzIconModule } from 'ng-zorro-antd/icon'; import { NzTagModule } from 'ng-zorro-antd/tag'; import { NzSpinModule } from 'ng-zorro-antd/spin'; import { ApiService } from '../../../core/services/api.service'; import { NotificationService } from '../../../core/services/notification.service'; import { KeyCacheService, ResolvedKey } from '../../../core/services/key-cache.service'; import { Message } from '../../../core/models'; import { RelativeTimePipe } from '../../../shared/pipes/relative-time.pipe'; import { MetadataGridComponent, MetadataValueComponent } from '../../../shared/components/metadata-grid'; @Component({ selector: 'app-message-detail', standalone: true, imports: [ CommonModule, DatePipe, NzCardModule, NzButtonModule, NzIconModule, NzTagModule, NzSpinModule, RouterLink, RelativeTimePipe, MetadataGridComponent, MetadataValueComponent, ], templateUrl: './message-detail.component.html', styleUrl: './message-detail.component.scss' }) export class MessageDetailComponent implements OnInit { private route = inject(ActivatedRoute); private router = inject(Router); private apiService = inject(ApiService); private notification = inject(NotificationService); private keyCacheService = inject(KeyCacheService); message = signal(null); resolvedKey = signal(null); loading = signal(true); deleting = signal(false); ngOnInit(): void { const messageId = this.route.snapshot.paramMap.get('id'); if (messageId) { this.loadMessage(messageId); } } loadMessage(messageId: string): void { this.loading.set(true); this.apiService.getMessage(messageId).subscribe({ next: (message) => { this.message.set(message); this.loading.set(false); this.resolveKey(message.used_key_id); }, error: () => { this.loading.set(false); } }); } private resolveKey(keyId: string): void { this.keyCacheService.resolveKey(keyId).subscribe({ next: (resolved) => this.resolvedKey.set(resolved) }); } goBack(): void { this.router.navigate(['/messages']); } deleteMessage(): void { const message = this.message(); if (!message) return; this.deleting.set(true); this.apiService.deleteMessage(message.message_id).subscribe({ next: () => { this.notification.success('Message deleted'); this.router.navigate(['/messages']); }, error: () => { this.deleting.set(false); } }); } getPriorityLabel(priority: number): string { switch (priority) { case 0: return 'Low'; case 1: return 'Normal'; case 2: return 'High'; default: return 'Unknown'; } } getPriorityColor(priority: number): string { switch (priority) { case 0: return 'default'; case 1: return 'blue'; case 2: return 'red'; default: return 'default'; } } }