import { Injectable, inject, resource } from '@angular/core';
import { logger } from '@isa/core/logging';
import { CrmSearchService } from '@isa/crm/data-access';
import { KeyValueDTOOfStringAndInteger } from '@generated/swagger/crm-api';

@Injectable()
export class CustomerBookingReasonsResource {
  readonly #crmSearchService = inject(CrmSearchService);
  readonly #logger = logger(() => ({
    context: 'CustomerBookingReasonsResource',
  }));

  readonly resource = resource({
    loader: async ({
      abortSignal,
    }): Promise<KeyValueDTOOfStringAndInteger[] | undefined> => {
      this.#logger.debug('Loading Booking Reasons');

      const reasons =
        await this.#crmSearchService.fetchBookingReasons(abortSignal);

      this.#logger.debug('Booking Reasons loaded', () => ({
        count: reasons.length,
      }));

      return reasons;
    },
  });
}
