/**
 * Technology Registry
 * Maps technologies to their languages, runtimes, and package managers
 */

export interface TechnologyConfig {
  language: string;
  runtime: string;
  packageManagers: string[];
  defaultPackageManager: string;
  buildTool?: string;
  testFrameworks?: string[];
}

export const TECHNOLOGY_REGISTRY: Record<string, TechnologyConfig> = {
  // JavaScript/TypeScript (Node.js)
  'react': {
    language: 'javascript',
    runtime: 'node',
    packageManagers: ['npm', 'yarn', 'pnpm', 'bun'],
    defaultPackageManager: 'npm',
    testFrameworks: ['vitest', 'jest', 'playwright'],
  },
  'vue': {
    language: 'javascript',
    runtime: 'node',
    packageManagers: ['npm', 'yarn', 'pnpm', 'bun'],
    defaultPackageManager: 'npm',
    testFrameworks: ['vitest', 'jest', 'cypress'],
  },
  'nextjs': {
    language: 'typescript',
    runtime: 'node',
    packageManagers: ['npm', 'yarn', 'pnpm', 'bun'],
    defaultPackageManager: 'npm',
    testFrameworks: ['vitest', 'jest', 'playwright'],
  },
  'typescript/express': {
    language: 'typescript',
    runtime: 'node',
    packageManagers: ['npm', 'yarn', 'pnpm', 'bun'],
    defaultPackageManager: 'npm',
    testFrameworks: ['jest', 'supertest'],
  },
  'typescript/node': {
    language: 'typescript',
    runtime: 'node',
    packageManagers: ['npm', 'yarn', 'pnpm', 'bun'],
    defaultPackageManager: 'npm',
    testFrameworks: ['jest'],
  },

  // Python
  'python/django': {
    language: 'python',
    runtime: 'python',
    packageManagers: ['pip', 'poetry', 'pipenv', 'conda'],
    defaultPackageManager: 'pip',
    testFrameworks: ['pytest', 'unittest'],
  },
  'python/fastapi': {
    language: 'python',
    runtime: 'python',
    packageManagers: ['pip', 'poetry', 'pipenv', 'conda'],
    defaultPackageManager: 'poetry',
    testFrameworks: ['pytest'],
  },
  'python/flask': {
    language: 'python',
    runtime: 'python',
    packageManagers: ['pip', 'poetry', 'pipenv'],
    defaultPackageManager: 'pip',
    testFrameworks: ['pytest', 'unittest'],
  },

  // Go
  'go': {
    language: 'go',
    runtime: 'go',
    packageManagers: ['go'], // Built-in go modules
    defaultPackageManager: 'go',
    testFrameworks: ['testing'],
  },
  'go/fiber': {
    language: 'go',
    runtime: 'go',
    packageManagers: ['go'],
    defaultPackageManager: 'go',
    testFrameworks: ['testing'],
  },
  'go/gin': {
    language: 'go',
    runtime: 'go',
    packageManagers: ['go'],
    defaultPackageManager: 'go',
    testFrameworks: ['testing'],
  },

  // Rust
  'rust': {
    language: 'rust',
    runtime: 'rust',
    packageManagers: ['cargo'],
    defaultPackageManager: 'cargo',
    testFrameworks: ['cargo-test'],
  },

  // Ruby
  'ruby/rails': {
    language: 'ruby',
    runtime: 'ruby',
    packageManagers: ['gem', 'bundler'],
    defaultPackageManager: 'bundler',
    testFrameworks: ['rspec', 'minitest'],
  },

  // PHP
  'php/laravel': {
    language: 'php',
    runtime: 'php',
    packageManagers: ['composer'],
    defaultPackageManager: 'composer',
    testFrameworks: ['phpunit'],
  },

  // Java
  'java/spring': {
    language: 'java',
    runtime: 'jvm',
    packageManagers: ['maven', 'gradle'],
    defaultPackageManager: 'maven',
    buildTool: 'maven',
    testFrameworks: ['junit', 'spring-test'],
  },

  // .NET
  'csharp/dotnet': {
    language: 'csharp',
    runtime: 'dotnet',
    packageManagers: ['nuget'],
    defaultPackageManager: 'nuget',
    buildTool: 'dotnet',
    testFrameworks: ['xunit', 'nunit'],
  },

  // Database (no package manager, but has tooling)
  'postgresql': {
    language: 'sql',
    runtime: 'postgresql',
    packageManagers: [],
    defaultPackageManager: '',
    testFrameworks: [],
  },
  'mongodb': {
    language: 'javascript',
    runtime: 'mongodb',
    packageManagers: [],
    defaultPackageManager: '',
    testFrameworks: [],
  },
  'mysql': {
    language: 'sql',
    runtime: 'mysql',
    packageManagers: [],
    defaultPackageManager: '',
    testFrameworks: [],
  },
};

/**
 * Get technology configuration
 */
export function getTechnologyConfig(technology: string): TechnologyConfig | undefined {
  return TECHNOLOGY_REGISTRY[technology];
}

/**
 * Get package manager for a technology (with override support)
 */
export function getPackageManagerForTechnology(
  technology: string,
  override?: string
): string {
  const config = getTechnologyConfig(technology);

  if (!config) {
    // Unknown technology, default to npm
    return 'npm';
  }

  // If override is specified and valid for this technology, use it
  if (override && config.packageManagers.includes(override)) {
    return override;
  }

  // Use default for this technology
  return config.defaultPackageManager;
}

/**
 * Get language from technology
 */
export function getLanguageFromTechnology(technology: string): string {
  const config = getTechnologyConfig(technology);
  return config?.language || 'unknown';
}

/**
 * Get runtime from technology
 */
export function getRuntimeFromTechnology(technology: string): string {
  const config = getTechnologyConfig(technology);
  return config?.runtime || 'unknown';
}

/**
 * Check if technology uses Node.js
 */
export function isNodeTechnology(technology: string): boolean {
  const config = getTechnologyConfig(technology);
  return config?.runtime === 'node';
}

/**
 * Get valid package managers for a technology
 */
export function getValidPackageManagers(technology: string): string[] {
  const config = getTechnologyConfig(technology);
  return config?.packageManagers || [];
}

/**
 * Validate package manager for technology
 */
export function isValidPackageManager(
  technology: string,
  packageManager: string
): boolean {
  const valid = getValidPackageManagers(technology);
  return valid.includes(packageManager);
}
