import { Injectable, computed, inject } from '@angular/core';
import { ConfigService } from './config.service';
import { DEFAULT_MARKETPLACE_FEATURES_CONFIG, DEFAULT_USER_EXPERIENCE_CONFIG, MarketplaceFeaturesConfig } from '../../shared/models/config';

@Injectable({ providedIn: 'root' })
export class FeatureConfigService {
  private readonly configService = inject(ConfigService);

  readonly features = computed<Required<MarketplaceFeaturesConfig>>(() => {
    this.configService.bootstrapRevision();
    const snapshot = this.configService.getBootstrapSnapshot() as any;
    const features = snapshot?.features ?? {};
    const userExperience = snapshot?.userExperience ?? {};
    const productPage = snapshot?.productPage ?? {};

    return {
      ...DEFAULT_MARKETPLACE_FEATURES_CONFIG,
      ...features,
      wishlist: features.wishlist ?? userExperience.wishlist?.enabled ?? DEFAULT_USER_EXPERIENCE_CONFIG.wishlist.enabled,
      compare: features.compare ?? userExperience.compare?.enabled ?? DEFAULT_USER_EXPERIENCE_CONFIG.compare.enabled,
      recentlyViewed: features.recentlyViewed ?? userExperience.recentlyViewed?.enabled ?? DEFAULT_USER_EXPERIENCE_CONFIG.recentlyViewed.enabled,
      searchHistory: features.searchHistory ?? snapshot?.catalog?.searchHistoryEnabled ?? true,
      recentlySearched: features.recentlySearched ?? snapshot?.catalog?.searchHistoryEnabled ?? true,
      share: features.share ?? userExperience.share?.enabled ?? DEFAULT_USER_EXPERIENCE_CONFIG.share.enabled,
      reviews: features.reviews ?? productPage.reviews?.enabled ?? true,
      questions: features.questions ?? productPage.questions?.enabled ?? true,
      ratings: features.ratings ?? snapshot?.catalog?.showRatings ?? true,
      availability: features.availability ?? snapshot?.catalog?.showAvailability ?? true,
      discounts: features.discounts ?? snapshot?.catalog?.showDiscounts ?? true,
      recommendations: features.recommendations ?? true,
      badges: features.badges ?? true,
    };
  });
}
