import { SmartComponentSelector } from './smart-selector';
import { componentRegistry, ComponentMetadata } from './component-registry';
import { FuzzyMatcher } from './fuzzy-matcher';

// Main SmartSelector interface for comprehensive component management
export const SmartSelector = {
  // Main component selection method
  select: SmartComponentSelector.select,

  // Get components by use case
  getComponentsByUseCase: (useCase: string): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.useCases.some(uc => uc.includes(useCase))
    );
  },

  // Get components by tag
  getComponentsByTag: (tag: string): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.tags.includes(tag)
    );
  },

  // Get components by category
  getComponentsByCategory: (category: string): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.category === category
    );
  },

  // Get components by complexity level
  getComponentsByComplexity: (complexity: 'simple' | 'medium' | 'complex'): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.complexity === complexity
    );
  },

  // Get components with animations
  getAnimatedComponents: (): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.animationLevel === 'high' || c.animationLevel === 'medium'
    );
  },

  // Get components without animations
  getStaticComponents: (): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.animationLevel === 'low'
    );
  },

  // Get total count of components
  getTotalComponentsCount: (): number => {
    return Object.keys(componentRegistry).length;
  },

  // Get total count of animated components
  getAnimatedComponentsCount: (): number => {
    return Object.values(componentRegistry).filter(c => 
      c.animationLevel === 'high' || c.animationLevel === 'medium'
    ).length;
  },

  // Get total count of static components
  getStaticComponentsCount: (): number => {
    return Object.values(componentRegistry).filter(c => 
      c.animationLevel === 'low'
    ).length;
  },

  // Get components with gradients
  getGradientComponents: (): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.hasGradient
    );
  },

  // Get composite components
  getCompositeComponents: (): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => 
      c.isComposite
    );
  },

  // Resolve component name using fuzzy matching
  resolve: (componentName: string) => {
    return FuzzyMatcher.findBestMatch(componentName);
  },

  // Get component statistics
  getComponentStatistics: () => {
    const total = Object.keys(componentRegistry).length;
    const byCategory = Object.values(componentRegistry).reduce((acc, c) => {
      acc[c.category] = (acc[c.category] || 0) + 1;
      return acc;
    }, {} as Record<string, number>);
    
    const byComplexity = Object.values(componentRegistry).reduce((acc, c) => {
      acc[c.complexity] = (acc[c.complexity] || 0) + 1;
      return acc;
    }, {} as Record<string, number>);

    const animated = Object.values(componentRegistry).filter(c => 
      c.animationLevel === 'high' || c.animationLevel === 'medium'
    ).length;
    const withGradients = Object.values(componentRegistry).filter(c => c.hasGradient).length;
    const composite = Object.values(componentRegistry).filter(c => c.isComposite).length;

    return {
      total,
      byCategory,
      byComplexity,
      animated,
      withGradients,
      composite,
      primitives: total - composite
    };
  },

  // Search components with multiple criteria
  search: (criteria: {
    useCase?: string;
    tags?: string[];
    category?: string;
    complexity?: string;
    hasAnimation?: boolean;
    hasGradient?: boolean;
    isComposite?: boolean;
  }): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(component => {
      if (criteria.useCase && !component.useCases.some(uc => uc.includes(criteria.useCase!))) {
        return false;
      }
      if (criteria.tags && !criteria.tags.some(tag => component.tags.includes(tag))) {
        return false;
      }
      if (criteria.category && component.category !== criteria.category) {
        return false;
      }
      if (criteria.complexity && component.complexity !== criteria.complexity) {
        return false;
      }
      if (criteria.hasAnimation && (component.animationLevel === 'low')) {
        return false; // Skip low animation components when animation is required
      }
      if (criteria.hasGradient && !component.hasGradient) {
        return false;
      }
      if (criteria.isComposite && !component.isComposite) {
        return false;
      }
      return true;
    });
  },

  // Get all available categories
  getCategories: (): string[] => {
    return [...new Set(Object.values(componentRegistry).map(c => c.category))];
  },

  // Get all available tags
  getTags: (): string[] => {
    const allTags = Object.values(componentRegistry).flatMap(c => c.tags);
    return [...new Set(allTags)];
  },

  // Get component by exact name
  getComponentByName: (name: string): ComponentMetadata | undefined => {
    return componentRegistry[name];
  },

  // Get components with high priority
  getHighPriorityComponents: (): ComponentMetadata[] => {
    return Object.values(componentRegistry).filter(c => c.priority === 1);
  }
};

// Export the smart component selector for use in the application
export { SmartComponentSelector };

// Export the main SmartSelector interface as default
export default SmartSelector;
