import { ChangeDetectionStrategy, Component, inject, signal } 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';
import { ConfirmDialogComponent } from '../../../../shared/ui/confirm-dialog/confirm-dialog.component';
import { TranslatePipe } from '../../../../i18n/translate.pipe';
import { DialogComponent } from '../../../../shared/ui/dialog/dialog.component';
import { ButtonComponent } from '../../../../shared/ui/button/button.component';

@Component({
  selector: 'app-admin-products-list-page',
  standalone: true,
  imports: [AdminProductsListComponent, ConfirmDialogComponent, TranslatePipe, DialogComponent, ButtonComponent],
  template: `<app-admin-products-list
    [products]="facade.products()"
    [categories]="facade.categories()"
    [filters]="facade.filters()"
    [total]="facade.total()"
    [selectedIds]="facade.selectedIds()"
    [loading]="facade.loading()"
    [error]="facade.error()"
    [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)="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)="bulkDelete()"
    (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)" />
    <app-confirm-dialog
      [open]="!!pendingDeleteId()"
      [titleText]="'adminProducts.deleteTitle' | translate"
      [message]="'adminProducts.confirmDelete' | translate"
      [destructive]="true"
      (confirmed)="confirmDelete()"
      (cancelled)="pendingDeleteId.set(null)" />
    <app-confirm-dialog
      [open]="bulkDeleteConfirmOpen()"
      [titleText]="'adminProducts.deleteTitle' | translate"
      [message]="'adminProducts.confirmBulkDelete' | translate"
      [destructive]="true"
      (confirmed)="confirmBulkDelete()"
      (cancelled)="bulkDeleteConfirmOpen.set(false)" />
    @if (facade.mutationError()) {
      <app-dialog [open]="true" [titleText]="'common.errorTitle' | translate" size="sm" (closed)="facade.mutationError.set(null)">
        <p>{{ 'common.errorDescription' | translate }}</p>
        <div class="app-confirm-dialog__actions">
          <app-button variant="primary" (click)="facade.mutationError.set(null)">{{ 'common.confirm' | translate }}</app-button>
        </div>
      </app-dialog>
    }`,
  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);

  readonly pendingDeleteId = signal<string | null>(null);
  readonly bulkDeleteConfirmOpen = signal(false);

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

  deleteOne(id: string): void {
    this.pendingDeleteId.set(id);
  }

  confirmDelete(): void {
    const id = this.pendingDeleteId();
    if (id) {
      this.facade.deleteOne(id);
    }
    this.pendingDeleteId.set(null);
  }

  bulkDelete(): void {
    this.bulkDeleteConfirmOpen.set(true);
  }

  confirmBulkDelete(): void {
    this.facade.applyBulkDelete();
    this.bulkDeleteConfirmOpen.set(false);
  }

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