import { ChangeDetectionStrategy, Component, computed, inject, signal, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { forkJoin } from 'rxjs'; import { ReportsService } from '../../services/reports/reports.service'; import { ReportsView } from '../../services/reports/reports.types'; import { BarChartComponent, BarChartItem } from '../../shared/ui/charts/bar-chart.component'; import { DoughnutChartComponent, DoughnutSegment } from '../../shared/ui/charts/doughnut-chart.component'; import { LineChartComponent, LinePoint } from '../../shared/ui/charts/line-chart.component'; import { CHART_PALETTE } from '../../shared/utils/chart-palette'; import { formatCount, formatDecimal, formatPercent } from '../../shared/utils/format.util'; type ReportTabId = 'questions' | 'performance'; @Component({ selector: 'app-reports', standalone: true, imports: [CommonModule, BarChartComponent, DoughnutChartComponent, LineChartComponent], changeDetection: ChangeDetectionStrategy.OnPush, templateUrl: './reports.page.html', styleUrl: './reports.page.css', }) export class ReportsPage implements OnInit { private readonly reports = inject(ReportsService); readonly tabs: { id: ReportTabId; label: string; icon: string }[] = [ { id: 'questions', label: 'پرسش‌های کاربران', icon: 'M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z', }, { id: 'performance', label: 'عملکرد و روند', icon: 'M22 12h-4l-3 9L9 3l-3 9H2', }, ]; readonly activeTab = signal('questions'); readonly loading = signal(true); readonly loadError = signal(false); readonly data = signal(null); readonly summary = computed(() => this.data()?.summary ?? null); readonly ai = computed(() => this.data()?.aiPerformance ?? null); readonly topQuestions = computed(() => { const questions = this.data()?.topQuestions ?? []; return questions.map((question, index) => ({ label: question.text, value: question.count, color: CHART_PALETTE[index % CHART_PALETTE.length], hint: `دسته: ${question.category}`, })); }); readonly categories = computed(() => { const shares = this.data()?.categories ?? []; return shares.map((share, index) => ({ label: share.category, value: share.count, color: CHART_PALETTE[index % CHART_PALETTE.length], })); }); readonly trend = computed(() => { const points = this.data()?.trend ?? []; return points.map((point) => ({ label: point.label, value: point.count })); }); readonly channels = computed(() => { const channels = this.data()?.aiPerformance.channels ?? []; return channels.map((channel, index) => ({ label: channel.label, value: channel.count, color: CHART_PALETTE[index % CHART_PALETTE.length], })); }); readonly aiAnswerRate = computed(() => { const summary = this.summary(); if (!summary || summary.totalQuestions === 0) return 0; return (summary.aiAnswered / summary.totalQuestions) * 100; }); readonly faqBasedRate = computed(() => { const ai = this.ai(); if (!ai || ai.totalResponses === 0) return 0; return (ai.faqBasedResponses / ai.totalResponses) * 100; }); readonly trendTotal = computed(() => this.trend().reduce((sum, point) => sum + point.value, 0)); readonly trendPeak = computed(() => { const points = this.trend(); if (points.length === 0) return null; return points.reduce((peak, point) => (point.value > peak.value ? point : peak), points[0]); }); ngOnInit(): void { this.load(); } selectTab(id: ReportTabId): void { this.activeTab.set(id); } load(): void { this.loading.set(true); this.loadError.set(false); forkJoin({ topQuestions: this.reports.getTopQuestions(), summary: this.reports.getQuestionSummary(), categories: this.reports.getCategoryDistribution(), trend: this.reports.getUsageTrend(), aiPerformance: this.reports.getAiPerformance(), }).subscribe({ next: (data) => { this.data.set(data); this.loading.set(false); }, error: () => { this.data.set(null); this.loading.set(false); this.loadError.set(true); }, }); } retry(): void { this.load(); } formatCount(value: number): string { return formatCount(value); } formatDecimal(value: number): string { return formatDecimal(value); } formatPercent(value: number): string { return formatPercent(value); } }