import { Injectable } from '@nestjs/common';
import { IPlatformAdapter } from './IPlatformAdapter';
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';
import { Logger } from '@nestjs/common';

@Injectable()
export class PlatformAdapterFactory {
  private readonly logger = new Logger(PlatformAdapterFactory.name);

  constructor(
    private amazonAdapter: AmazonAdapter,
    private shopeeAdapter: ShopeeAdapter,
    private aliExpressAdapter: AliExpressAdapter,
    private tiktokAdapter: TikTokAdapter,
    private ebayAdapter: EbayAdapter,
    private genericAdapter: GenericAdapter
  ) {}

  /**
   * 创建平台适配器
   * @param platform 平台名称
   * @returns 平台适配器实例
   */
  createAdapter(platform: string): IPlatformAdapter {
    this.logger.log(`创建平台适配器: ${platform}`);

    switch (platform.toLowerCase()) {
      case PlatformType.AMAZON:
        return this.amazonAdapter;
      case PlatformType.SHOPEE:
        return this.shopeeAdapter;
      case PlatformType.ALIEXPRESS:
        return this.aliExpressAdapter;
      case PlatformType.TIKTOK:
        return this.tiktokAdapter;
      case PlatformType.EBAY:
        return this.ebayAdapter;
      default:
        this.logger.warn(`平台 ${platform} 没有专用适配器，使用通用适配器`);
        return this.genericAdapter;
    }
  }

  /**
   * 获取支持的平台列表
   * @returns 支持的平台列表
   */
  getSupportedPlatforms(): string[] {
    return Object.values(PlatformType);
  }

  /**
   * 检查平台是否支持
   * @param platform 平台名称
   * @returns 是否支持
   */
  isPlatformSupported(platform: string): boolean {
    return Object.values(PlatformType).includes(platform.toLowerCase() as PlatformType);
  }
}
