import { Test, TestingModule } from '@nestjs/testing';
import { PlatformAdapterFactory } from './PlatformAdapterFactory';
import { AmazonAdapter } from './AmazonAdapter';
import { ShopeeAdapter } from './ShopeeAdapter';
import { AliExpressAdapter } from './AliExpressAdapter';
import { TikTokAdapter } from './TikTokAdapter';
import { EbayAdapter } from './EbayAdapter';
import { GenericAdapter } from './GenericAdapter';
import { PlatformType } from '../../../types/enums/PlatformType';

describe('PlatformAdapterFactory', () => {
  let factory: PlatformAdapterFactory;
  let amazonAdapter: AmazonAdapter;
  let shopeeAdapter: ShopeeAdapter;
  let aliExpressAdapter: AliExpressAdapter;
  let tiktokAdapter: TikTokAdapter;
  let ebayAdapter: EbayAdapter;
  let genericAdapter: GenericAdapter;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        PlatformAdapterFactory,
        AmazonAdapter,
        ShopeeAdapter,
        AliExpressAdapter,
        TikTokAdapter,
        EbayAdapter,
        GenericAdapter,
      ],
    }).compile();

    factory = module.get<PlatformAdapterFactory>(PlatformAdapterFactory);
    amazonAdapter = module.get<AmazonAdapter>(AmazonAdapter);
    shopeeAdapter = module.get<ShopeeAdapter>(ShopeeAdapter);
    aliExpressAdapter = module.get<AliExpressAdapter>(AliExpressAdapter);
    tiktokAdapter = module.get<TikTokAdapter>(TikTokAdapter);
    ebayAdapter = module.get<EbayAdapter>(EbayAdapter);
    genericAdapter = module.get<GenericAdapter>(GenericAdapter);
  });

  describe('createAdapter', () => {
    it('should return AmazonAdapter for amazon platform', () => {
      const adapter = factory.createAdapter(PlatformType.AMAZON);
      expect(adapter).toBeInstanceOf(AmazonAdapter);
    });

    it('should return ShopeeAdapter for shopee platform', () => {
      const adapter = factory.createAdapter(PlatformType.SHOPEE);
      expect(adapter).toBeInstanceOf(ShopeeAdapter);
    });

    it('should return AliExpressAdapter for aliexpress platform', () => {
      const adapter = factory.createAdapter(PlatformType.ALIEXPRESS);
      expect(adapter).toBeInstanceOf(AliExpressAdapter);
    });

    it('should return TikTokAdapter for tiktok platform', () => {
      const adapter = factory.createAdapter(PlatformType.TIKTOK);
      expect(adapter).toBeInstanceOf(TikTokAdapter);
    });

    it('should return EbayAdapter for ebay platform', () => {
      const adapter = factory.createAdapter(PlatformType.EBAY);
      expect(adapter).toBeInstanceOf(EbayAdapter);
    });

    it('should return GenericAdapter for unknown platform', () => {
      const adapter = factory.createAdapter('unknown');
      expect(adapter).toBeInstanceOf(GenericAdapter);
    });

    it('should handle case-insensitive platform names', () => {
      const adapter = factory.createAdapter('AMAZON');
      expect(adapter).toBeInstanceOf(AmazonAdapter);
    });
  });

  describe('getSupportedPlatforms', () => {
    it('should return all supported platforms', () => {
      const platforms = factory.getSupportedPlatforms();
      expect(platforms).toBeInstanceOf(Array);
      expect(platforms).toContain(PlatformType.AMAZON);
      expect(platforms).toContain(PlatformType.SHOPEE);
      expect(platforms).toContain(PlatformType.ALIEXPRESS);
      expect(platforms).toContain(PlatformType.TIKTOK);
      expect(platforms).toContain(PlatformType.EBAY);
    });
  });

  describe('isPlatformSupported', () => {
    it('should return true for supported platforms', () => {
      expect(factory.isPlatformSupported(PlatformType.AMAZON)).toBe(true);
      expect(factory.isPlatformSupported(PlatformType.SHOPEE)).toBe(true);
      expect(factory.isPlatformSupported(PlatformType.ALIEXPRESS)).toBe(true);
      expect(factory.isPlatformSupported(PlatformType.TIKTOK)).toBe(true);
      expect(factory.isPlatformSupported(PlatformType.EBAY)).toBe(true);
    });

    it('should return false for unsupported platforms', () => {
      expect(factory.isPlatformSupported('unknown')).toBe(false);
    });

    it('should handle case-insensitive platform names', () => {
      expect(factory.isPlatformSupported('AMAZON')).toBe(true);
    });
  });
});
