import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
  resource,
} from '@angular/core';
import { isaOtherInfo } from '@isa/icons';
import { RemissionReturnReceiptService } from '@isa/remission/data-access';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { subDays } from 'date-fns';

// TODO: #5136 - Code anpassen sobald Ticket #5215 umgesetzt ist
// HTML in remission-list.component.html ist auskommentiert
@Component({
  selector: 'remi-remission-processed-hint',
  templateUrl: './remission-processed-hint.component.html',
  styleUrls: ['./remission-processed-hint.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [NgIcon],
  providers: [provideIcons({ isaOtherInfo })],
})
export class RemissionProcessedHintComponent {
  readonly #remissionReturnReceiptService = inject(
    RemissionReturnReceiptService,
  );

  /**
   * Threshold in days where at least one remission has been processed.
   * If no remission has been processed in the last 7 days,
   * the hint will be displayed.
   */
  readonly daysThreshold = input<number>(7);

  /**
   * Resource that fetches remission return receipts.
   * Automatically loads when the component is initialized.
   */
  readonly returnReceiptsResource = resource({
    params: () => ({
      returncompleted: false,
      start: subDays(new Date(), this.daysThreshold()),
    }),
    loader: ({ params, abortSignal }) =>
      this.#remissionReturnReceiptService.fetchRemissionReturnReceipts(
        params,
        abortSignal,
      ),
  });

  readonly showHint = computed(() => {
    const receipts = this.returnReceiptsResource.value();
    return receipts && receipts.length === 0;
  });

  readonly error = this.returnReceiptsResource.error;

  readonly isLoading = this.returnReceiptsResource.isLoading;

  readonly retry = () => {
    this.returnReceiptsResource.reload();
  };
}
