import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterLink } from '@angular/router';
import { AdminAnalyticsFacade } from '../facade/admin-analytics.facade';
import { AdminAnalyticsDateRange } from '../models/admin-analytics.model';
import { TranslatePipe } from '../../../../i18n/translate.pipe';
import { ButtonComponent } from '../../../../shared/ui/button/button.component';
import { BadgeComponent } from '../../../../shared/ui/badge/badge.component';
import { TableComponent } from '../../../../shared/ui/table/table.component';
import { SkeletonComponent } from '../../../../shared/ui/skeleton/skeleton.component';
import { EmptyStateComponent } from '../../../../shared/ui/empty-state/empty-state.component';

export type AdminAnalyticsTab = 'overview' | 'health' | 'products' | 'customers' | 'search' | 'traffic' | 'recommendations';

const TABS: AdminAnalyticsTab[] = ['overview', 'health', 'products', 'customers', 'search', 'traffic', 'recommendations'];

@Component({
  selector: 'app-admin-analytics-page',
  standalone: true,
  imports: [CommonModule, RouterLink, TranslatePipe, ButtonComponent, BadgeComponent, TableComponent, SkeletonComponent, EmptyStateComponent],
  templateUrl: './admin-analytics-page.component.html',
  styleUrls: ['./admin-analytics-page.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AdminAnalyticsPageComponent {
  readonly facade = inject(AdminAnalyticsFacade);

  readonly ranges: AdminAnalyticsDateRange[] = [7, 30, 90];
  readonly tabs = TABS;
  readonly activeTab = signal<AdminAnalyticsTab>('overview');

  constructor() {
    this.facade.load();
  }

  selectTab(tab: AdminAnalyticsTab): void {
    this.activeTab.set(tab);
  }

  barHeight(value: number): number {
    return Math.max(2, Math.round((value / this.facade.maxSeriesValue()) * 100));
  }

  healthStatusVariant(status: string): 'success' | 'warning' | 'danger' | 'neutral' {
    switch (status) {
      case 'healthy': return 'success';
      case 'attention': return 'warning';
      case 'unhealthy': return 'danger';
      default: return 'neutral';
    }
  }

  severityVariant(severity: string): 'warning' | 'danger' | 'info' {
    return severity === 'critical' ? 'danger' : severity === 'warning' ? 'warning' : 'info';
  }

  exportCsv(): void {
    this.download(this.facade.exportCsv(), 'sales.csv');
  }

  exportTopProductsCsv(): void {
    this.download(this.facade.exportTopProductsCsv(), 'top-products.csv');
  }

  exportHealthCsv(): void {
    this.download(this.facade.exportHealthCsv(), 'marketplace-health.csv');
  }

  print(): void {
    window.print();
  }

  private download(csv: string, filename: string): void {
    const blob = new Blob([csv], { type: 'text/csv' });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = filename;
    link.click();
    URL.revokeObjectURL(url);
  }
}
