import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { AdminAnalyticsFacade } from '../../analytics/facade/admin-analytics.facade';
import { TranslatePipe } from '../../../../i18n/translate.pipe';
import { ButtonComponent } from '../../../../shared/ui/button/button.component';
import { SkeletonComponent } from '../../../../shared/ui/skeleton/skeleton.component';
import { EmptyStateComponent } from '../../../../shared/ui/empty-state/empty-state.component';

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

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

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

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

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

  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);
  }
}
