import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { AdminModerationFacade, AdminReviewColumn, ALL_REVIEW_COLUMNS } from '../facade/admin-moderation.facade';
import { AdminReviewStatus } from '../models/admin-review.model';
import { TranslatePipe } from '../../../../i18n/translate.pipe';
import { LanguageService } from '../../../../services/language.service';
import { ButtonComponent } from '../../../../shared/ui/button/button.component';
import { InputComponent } from '../../../../shared/ui/input/input.component';
import { BadgeComponent } from '../../../../shared/ui/badge/badge.component';
import { TableComponent } from '../../../../shared/ui/table/table.component';
import { PaginationComponent } from '../../../../shared/ui/pagination/pagination.component';
import { EmptyStateComponent } from '../../../../shared/ui/empty-state/empty-state.component';
import { SkeletonComponent } from '../../../../shared/ui/skeleton/skeleton.component';
import { CardComponent } from '../../../../shared/ui/card/card.component';
import { ModerationDashboardComponent } from '../components/moderation-dashboard/moderation-dashboard.component';
import { ReviewHealthWidgetComponent, ReviewHealthItem } from '../components/review-health-widget/review-health-widget.component';
import { ConfirmDialogComponent } from '../../../../shared/ui/confirm-dialog/confirm-dialog.component';

@Component({
  selector: 'app-admin-reviews-list-page',
  standalone: true,
  imports: [CommonModule, FormsModule, TranslatePipe, ButtonComponent, InputComponent, BadgeComponent, TableComponent, PaginationComponent, EmptyStateComponent, SkeletonComponent, CardComponent, ModerationDashboardComponent, ReviewHealthWidgetComponent, ConfirmDialogComponent],
  templateUrl: './admin-reviews-list-page.component.html',
  styleUrls: ['./admin-reviews-list-page.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AdminReviewsListPageComponent {
  readonly facade = inject(AdminModerationFacade);
  private readonly router = inject(Router);
  private readonly languageService = inject(LanguageService);

  readonly statuses = ['all', 'pending', 'approved', 'rejected', 'spam'] as const;
  readonly ratings = ['all', 5, 4, 3, 2, 1] as const;
  readonly allColumns = ALL_REVIEW_COLUMNS;
  protected readonly columnsPanelOpen = signal(false);
  protected readonly bulkStatusValue = signal<AdminReviewStatus>('approved');
  protected readonly bulkDeleteConfirmOpen = signal(false);

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

  totalPages(): number {
    return Math.max(1, Math.ceil(this.facade.total() / this.facade.filters().pageSize));
  }

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

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

  view(id: string): void {
    void this.router.navigate([this.languageService.currentLanguage(), 'backoffice', 'moderation', id]);
  }

  viewReports(): void {
    void this.router.navigate([this.languageService.currentLanguage(), 'backoffice', 'moderation', 'reports']);
  }

  isSelected(id: string): boolean {
    return this.facade.selectedIds().includes(id);
  }

  isColumnVisible(column: AdminReviewColumn): boolean {
    return this.facade.visibleColumns().includes(column);
  }

  healthItems(review: Parameters<AdminModerationFacade['health']>[0]): ReviewHealthItem[] {
    const health = this.facade.health(review);
    return [
      { labelKey: 'adminModeration.healthRating', done: health.hasRating },
      { labelKey: 'adminModeration.healthText', done: health.hasText },
      { labelKey: 'adminModeration.healthMedia', done: health.hasMedia },
      { labelKey: 'adminModeration.healthModeration', done: health.isModerated },
      { labelKey: 'adminModeration.healthReportStatus', done: health.reportStatusClear },
      { labelKey: 'adminModeration.healthVisibility', done: health.isVisible },
    ];
  }

  applyBulkStatus(): void {
    this.facade.applyBulkStatus(this.bulkStatusValue());
  }
}
