/**
 * Validator Registry - Technology-Based Auto-Discovery
 *
 * Maps validator types to their modules for dynamic loading.
 * Organized by technology (blazor, css, architecture, etc.)
 *
 * @module validator-registry
 */

/**
 * Validator Metadata Registry
 *
 * Technology-scoped validator discovery.
 */
export const validatorMetadata = {
  // Blazor Validators
  'blazor': {
    technology: 'blazor',
    validators: [
      {
        id: 'blazor-validator',
        name: 'Blazor Patterns Validator',
        description: 'Validates Fluent UI Blazor patterns and components',
        module: () => import('../../lib/validators/blazor/blazor-validator.js'),
      },
      {
        id: 'blazor-state',
        name: 'Blazor State Validator',
        description: 'Validates Blazor state management patterns',
        module: () => import('../../lib/validators/blazor/blazor-state-validator.js'),
      },
      {
        id: 'blazor-concurrency',
        name: 'Blazor Concurrency Analyzer',
        description: 'Analyzes Blazor concurrency and DbContext issues',
        module: () => import('../../lib/validators/blazor/blazor-concurrency-analyzer.js'),
      },
    ],
  },

  // CSS Validators
  'css': {
    technology: 'css',
    validators: [
      {
        id: 'css-validator',
        name: 'CSS Validator',
        description: 'Validates CSS classes and design system compliance',
        module: () => import('../../lib/validators/css/css-validator.js'),
      },
    ],
  },

  // Architecture Validators
  'architecture': {
    technology: 'architecture',
    validators: [
      {
        id: 'architecture-validator',
        name: 'Architecture Validator',
        description: 'Validates architectural patterns (DI, CQRS, etc.)',
        module: () => import('../../lib/validators/architecture/architecture-validator.js'),
      },
    ],
  },

  // Design System Validators
  'design-system': {
    technology: 'design-system',
    validators: [
      {
        id: 'design-system-validator',
        name: 'Design System Validator',
        description: 'Validates design system compliance',
        module: () => import('../../lib/validators/design-system/design-system-validator.js'),
      },
    ],
  },

  // Content Validators
  'content': {
    technology: 'content',
    validators: [
      {
        id: 'content-validator',
        name: 'Content Validator',
        description: 'Validates content and specification format',
        module: () => import('../../lib/validators/content/content-validator.js'),
      },
    ],
  },

  // Contract Validators
  'contracts': {
    technology: 'contracts',
    validators: [
      {
        id: 'contract-compliance-validator',
        name: 'Contract Compliance Validator',
        description: 'Validates API contract compliance',
        module: () => import('../../lib/validators/contracts/contract-compliance-validator.js'),
      },
    ],
  },

  // Package Validators
  'packages': {
    technology: 'packages',
    validators: [
      {
        id: 'package-validator',
        name: 'Package Validator',
        description: 'Validates package dependencies and versions',
        module: () => import('../../lib/validators/packages/package-validator.js'),
      },
    ],
  },

  // UI Validators
  'ui': {
    technology: 'ui',
    validators: [
      {
        id: 'ui-contrast-validator',
        name: 'UI Contrast Validator',
        description: 'Validates UI contrast and accessibility',
        module: () => import('../../lib/validators/ui/ui-contrast-validator.js'),
      },
    ],
  },
};

/**
 * Load validator by ID
 *
 * @param {string} validatorId - Validator ID (e.g., 'blazor-validator')
 * @returns {Promise<Object>} Validator module
 */
export async function loadValidator(validatorId) {
  for (const techMeta of Object.values(validatorMetadata)) {
    const validator = techMeta.validators.find(v => v.id === validatorId);
    if (validator) {
      return await validator.module();
    }
  }

  throw new Error(`Validator not found: ${validatorId}`);
}

/**
 * Load all validators for a technology
 *
 * @param {string} technology - Technology name (e.g., 'blazor', 'css')
 * @returns {Promise<Object[]>} Array of validator modules
 */
export async function loadValidatorsForTechnology(technology) {
  const techMeta = validatorMetadata[technology];

  if (!techMeta) {
    throw new Error(`No validators found for technology: ${technology}`);
  }

  return await Promise.all(
    techMeta.validators.map(v => v.module())
  );
}

/**
 * Get all validator IDs
 *
 * @returns {string[]} Array of validator IDs
 */
export function getAllValidatorIds() {
  const ids = [];
  for (const techMeta of Object.values(validatorMetadata)) {
    ids.push(...techMeta.validators.map(v => v.id));
  }
  return ids;
}

/**
 * Get all technologies
 *
 * @returns {string[]} Array of technology names
 */
export function getAllTechnologies() {
  return Object.keys(validatorMetadata);
}

/**
 * Get validator metadata by ID
 *
 * @param {string} validatorId - Validator ID
 * @returns {Object|null} Validator metadata or null
 */
export function getValidatorMetadata(validatorId) {
  for (const techMeta of Object.values(validatorMetadata)) {
    const validator = techMeta.validators.find(v => v.id === validatorId);
    if (validator) {
      return { ...validator, technology: techMeta.technology };
    }
  }
  return null;
}
