import { createServiceFactory } from '@ngneat/spectator/jest';
import { receiptConfig, ReturnDetailsStore } from './return-details.store';
import { ReturnDetailsService } from '../services';
import { SessionStorageProvider } from '@isa/core/storage';
import { LoggingService } from '@isa/core/logging';
import { patchState } from '@ngrx/signals';
import { unprotected } from '@ngrx/signals/testing';
import { Receipt, ReceiptItem } from '../models';
import { addEntities } from '@ngrx/signals/entities';
import { ProductCategory } from '../questions';

describe('ReturnDetailsStore', () => {
  const createService = createServiceFactory({
    service: ReturnDetailsStore,
    mocks: [ReturnDetailsService, SessionStorageProvider, LoggingService],
  });

  it('should create an instance of ReturnDetailsStore', () => {
    const spectator = createService();
    expect(spectator.service).toBeTruthy();
  });

  describe('items', () => {
    it('should return the items from the receiptsEntities', () => {
      // Arrange
      const spectator = createService();

      const receiptItems = [
        {
          id: 1,
          quantity: { quantity: 1 },
          features: {
            category: ProductCategory.BookCalendar,
          } as { [key: string]: ProductCategory },
        } as ReceiptItem,
        {
          id: 2,
          quantity: { quantity: 2 },
          features: {
            category: ProductCategory.ElektronischeGeraete,
          } as { [key: string]: ProductCategory },
        } as ReceiptItem,
        {
          id: 3,
          quantity: { quantity: 3 },
          features: {
            category: ProductCategory.SpielwarenPuzzle,
          } as { [key: string]: ProductCategory },
        } as ReceiptItem,
        {
          id: 4,
          quantity: { quantity: 4 },
          features: {
            category: ProductCategory.TonDatentraeger,
          } as { [key: string]: ProductCategory },
        } as ReceiptItem,
      ];

      const receiptsEntities = [
        {
          id: 1,
          items: [{ data: receiptItems[0] }, { data: receiptItems[1] }],
        } as Receipt,
        {
          id: 2,
          items: [{ data: receiptItems[2] }, { data: receiptItems[3] }],
        } as Receipt,
      ] as Receipt[];

      patchState(
        unprotected(spectator.service),
        addEntities(receiptsEntities, receiptConfig),
      );

      // Act
      const items = spectator.service.items();

      // Assert
      expect(items).toEqual(receiptItems);
    });
  });
});
