import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { AdminCategoriesFacade } from '../facade/admin-categories.facade';
import { AdminCategoriesListComponent } from '../components/admin-categories-list.component';
import { LanguageService } from '../../../../services/language.service';
import { TranslateService } from '../../../../i18n/translate.service';

@Component({
  selector: 'app-admin-categories-list-page',
  standalone: true,
  imports: [AdminCategoriesListComponent],
  template: `<app-admin-categories-list
    [treeRows]="facade.visibleTreeRows()"
    [flatCategories]="facade.filteredCategories()"
    [allCategories]="facade.categories()"
    [filters]="facade.filters()"
    [loading]="facade.loading()"
    [viewMode]="facade.viewMode()"
    [density]="facade.density()"
    [visibleColumns]="facade.visibleColumns()"
    [selectedIds]="facade.selectedIds()"
    [dashboardStats]="facade.dashboardStats()"
    [health]="healthFn"
    [isExpanded]="isExpandedFn"
    [canDelete]="canDeleteFn"
    (filtersChange)="facade.updateFilters($event)"
    (create)="create()"
    (edit)="edit($event)"
    (delete)="deleteOne($event)"
    (restore)="facade.restoreOne($event)"
    (toggleVisible)="facade.setVisible($event.id, $event.visible)"
    (reorder)="facade.reorder($event.id, $event.targetId)"
    (toggleExpand)="facade.toggleExpanded($event)"
    (jumpToParent)="jumpToParent($event)"
    (expandAll)="facade.expandAll()"
    (collapseAll)="facade.collapseAll()"
    (selectionChange)="facade.toggleSelection($event.id, $event.checked)"
    (selectAll)="facade.toggleAll($event)"
    (bulkVisibility)="facade.applyBulkVisibility($event)"
    (bulkDelete)="facade.applyBulkDelete()"
    (bulkDuplicate)="facade.applyBulkDuplicate()"
    (bulkAssignParent)="facade.applyBulkAssignParent($event)"
    (bulkAssignImage)="facade.applyBulkAssignImage($event)"
    (bulkExport)="facade.exportSelectedAsCsv()"
    (viewModeChange)="facade.setViewMode($event)"
    (densityChange)="facade.setDensity($event)"
    (columnToggle)="facade.setColumnVisible($event.column, $event.visible)" />`,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AdminCategoriesListPageComponent {
  readonly facade = inject(AdminCategoriesFacade);
  private readonly router = inject(Router);
  private readonly languageService = inject(LanguageService);
  private readonly translate = inject(TranslateService);

  readonly healthFn = (category: Parameters<AdminCategoriesFacade['health']>[0]) => this.facade.health(category);
  readonly isExpandedFn = (id: string) => this.facade.isExpanded(id);
  readonly canDeleteFn = (id: string) => this.facade.canDelete(id);

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

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

  jumpToParent(id: string): void {
    const category = this.facade.categories().find(c => c.id === id);
    if (category?.parentId) {
      this.facade.expandAncestorsOf(id);
      queueMicrotask(() => document.getElementById(`category-tree-row-${category.parentId}`)?.focus());
    }
  }

  deleteOne(id: string): void {
    if (!this.facade.canDelete(id)) {
      window.alert(this.translate.t('adminCategories.deleteBlocked'));
      return;
    }
    if (window.confirm(this.translate.t('adminCategories.confirmDelete'))) {
      this.facade.deleteOne(id);
    }
  }

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