import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { AdminProductsFacade } from '../facade/admin-products.facade';
import { AdminProductsListComponent } from '../components/admin-products-list.component';
import { LanguageService } from '../../../../services/language.service';

@Component({
  selector: 'app-admin-products-list-page',
  standalone: true,
  imports: [AdminProductsListComponent],
  template: `<app-admin-products-list
    [products]="facade.products()"
    [categories]="facade.categories()"
    [filters]="facade.filters()"
    [total]="facade.total()"
    [selectedIds]="facade.selectedIds()"
    [loading]="facade.loading()"
    [infiniteScroll]="facade.infiniteScroll()"
    [viewMode]="facade.viewMode()"
    [density]="facade.density()"
    [visibleColumns]="facade.visibleColumns()"
    [dashboardStats]="facade.dashboardStats()"
    [health]="healthFn"
    (filtersChange)="facade.updateFilters($event)"
    (create)="create()"
    (edit)="edit($event)"
    (duplicate)="duplicate($event)"
    (delete)="facade.deleteOne($event)"
    (archive)="facade.archiveOne($event)"
    (restore)="facade.restoreOne($event)"
    (loadMore)="facade.loadMore()"
    (infiniteScrollToggle)="facade.setInfiniteScroll($event)"
    (selectionChange)="facade.toggleSelection($event.id, $event.checked)"
    (selectAll)="facade.toggleAll($event)"
    (bulkVisibility)="facade.applyBulkVisibility($event)"
    (bulkDelete)="facade.applyBulkDelete()"
    (bulkDuplicate)="facade.applyBulkDuplicate()"
    (bulkAssignCategory)="facade.applyBulkAssignCategory($event)"
    (bulkAssignTags)="facade.applyBulkAssignTags($event)"
    (bulkExport)="facade.exportSelectedAsCsv()"
    (viewModeChange)="facade.setViewMode($event)"
    (densityChange)="facade.setDensity($event)"
    (columnToggle)="facade.setColumnVisible($event.column, $event.visible)" />`,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AdminProductsListPageComponent {
  readonly facade = inject(AdminProductsFacade);
  private readonly router = inject(Router);
  private readonly languageService = inject(LanguageService);

  readonly healthFn = (product: Parameters<AdminProductsFacade['health']>[0]) => this.facade.health(product);

  constructor() {
    this.facade.loadCategories();
    this.facade.loadList();
    this.facade.loadDashboardStats();
  }

  create(): void { this.facade.startCreate(); void this.router.navigate([this.lang(), 'backoffice', 'products', 'create']); }
  edit(id: string): void { this.facade.loadForEdit(id, 'edit'); void this.router.navigate([this.lang(), 'backoffice', 'products', id, 'edit']); }
  duplicate(id: string): void { this.facade.loadForEdit(id, 'duplicate'); void this.router.navigate([this.lang(), 'backoffice', 'products', id, 'duplicate']); }

  private lang(): string {
    return this.languageService.currentLanguage();
  }
}
