import {
  ChangeDetectionStrategy,
  Component,
  input,
  computed,
  inject,
} from '@angular/core';
import { FilterService } from '@isa/shared/filter';
import { EmptyStateComponent, EmptyStateAppearance } from '@isa/ui/empty-state';

type EmptyState =
  | {
      title: string;
      description: string;
      appearance: EmptyStateAppearance;
    }
  | undefined;

@Component({
  selector: 'remi-feature-remission-list-empty-state',
  templateUrl: './remission-list-empty-state.component.html',
  styleUrl: './remission-list-empty-state.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [EmptyStateComponent],
})
export class RemissionListEmptyStateComponent {
  /**
   * FilterService instance for managing filter state and queries.
   * @private
   */
  #filterService = inject(FilterService);

  listFetching = input<boolean>();
  isDepartment = input<boolean>();
  isReloadSearch = input<boolean>();
  hasValidSearchTerm = input<boolean>();
  hits = input<number>();

  /**
   * Computed signal that determines the appropriate empty state to display
   * based on the current state of the remission list, search term, and filters.
   * @returns An EmptyState object with title, description, and appearance, or undefined if no empty state should be shown.
   * The priority for empty states is as follows:
   * 1. Department list with no department selected.
   * 2. All done state when the list is fully processed and no items remain.
   * 3. No results state when there are no items matching the current search and filters.
   * If none of these conditions are met, returns undefined.
   * @see EmptyStateAppearance for possible appearance values.
   * @remarks This logic ensures that the most relevant empty state is shown to the user based on their current context.
   */
  displayEmptyState = computed<EmptyState>(() => {
    if (!this.listFetching() && !this.hasValidSearchTerm()) {
      // Prio 1: Abteilungsremission - Es ist noch keine Abteilung ausgewählt
      if (
        this.isDepartment() &&
        !this.#filterService.query()?.filter['abteilungen']
      ) {
        return {
          title: 'Abteilung auswählen',
          description:
            'Wählen Sie zuerst eine Abteilung, anschließend werden die entsprechenden Positionen angezeigt.',
          appearance: EmptyStateAppearance.SelectAction,
        };
      }

      // Prio 2: Liste abgearbeitet und keine Artikel mehr vorhanden
      if (
        this.hits() === 0 &&
        this.isReloadSearch()
      ) {
        return {
          title: 'Alles erledigt',
          description: 'Hier gibt es gerade nichts zu tun',
          appearance: EmptyStateAppearance.AllDone,
        };
      }

      // Prio 3: Keine Ergebnisse bei leerem Suchbegriff (nur Filter gesetzt)
      if (this.hits() === 0) {
        return {
          title: 'Keine Suchergebnisse',
          description:
            'Bitte prüfen Sie die Schreibweise oder ändern Sie die Filtereinstellungen.',
          appearance: EmptyStateAppearance.NoResults,
        };
      }
    }
    return undefined;
  });
}
