/**
 * Centralized defaults for AI generation parameters
 */

import modelsData from "@/models.json";

export const AI_DEFAULTS = {
  // Temperature settings for different commands
  temperature: {
    commit: 0.3,      // Lower for more consistent commit messages
    review: 0.4,      // Slightly higher for more varied review feedback
    chat: 0.7,        // Higher for conversational responses
    default: 0.5,     // General default
  },
  
  // Max tokens for different commands (conservative defaults that work across models)
  maxTokens: {
    commit: 1024,     // Commit messages should be concise
    review: 2048,     // Reviews need more space for detailed feedback
    chat: 4096,       // Chat can be longer
    default: 2048,    // General default
  },
  
  // Default models per provider (from models.json)
  models: {
    gemini: modelsData.providers.gemini.defaultModel,
    anthropic: modelsData.providers.anthropic.defaultModel,
    openai: "gpt-4-turbo-preview", // OpenAI not in models.json yet
  }
} as const;

/**
 * Available command types for AI parameter configuration
 */
export type CommandType = keyof typeof AI_DEFAULTS.temperature;

/**
 * Validate temperature is within acceptable range
 * @param temp Temperature value to validate (0-2 for most providers)
 * @returns true if temperature is valid
 */
export function validateTemperature(temp: number): boolean {
  return temp >= 0 && temp <= 2;
}

/**
 * Get max tokens limit for a specific model
 * @param provider Provider name
 * @param model Model name (optional, uses default if not provided)
 * @returns Max tokens limit for the model
 */
export function getModelMaxTokens(
  provider: string,
  model?: string
): number {
  const providerData = (modelsData.providers as any)[provider];
  if (!providerData) {
    return 8192; // Conservative default
  }
  
  const modelName = model || providerData.defaultModel;
  const modelData = providerData.models[modelName];
  
  if (!modelData) {
    return 8192; // Conservative default
  }
  
  return modelData.maxOutputTokens;
}

/**
 * Validate max tokens is within acceptable range for a model
 * @param tokens Max tokens value to validate
 * @param provider Optional provider name
 * @param model Optional model name
 * @returns true if max tokens is valid
 */
export function validateMaxTokens(
  tokens: number,
  provider?: string,
  model?: string
): boolean {
  if (tokens <= 0) return false;
  
  // If no provider specified, use a conservative limit
  if (!provider) {
    return tokens <= 8192;
  }
  
  const maxLimit = getModelMaxTokens(provider, model);
  return tokens <= maxLimit;
}