import type { Attachment, ModelDefaults } from './types';

export type TaskType =
  | 'code'
  | 'code_review'
  | 'debugging'
  | 'reasoning'
  | 'math'
  | 'simple'
  | 'translation'
  | 'creative'
  | 'general';

export function classifyTask(input: string, attachments?: Attachment[]): TaskType {
  if (attachments?.some(a => a.type === 'image')) return 'code';

  const lower = input.toLowerCase();

  if (/```|function |class |const |import |def |=>|\{\s*\n/.test(input)) return 'code';
  if (/error:|traceback|exception|crashed?|enoent|sigterm/.test(lower)) return 'debugging';

  if (/review|refactor|optimize|lint/.test(lower)) return 'code_review';
  if (/debug|fix.*bug|why.*(fail|break|wrong)/.test(lower)) return 'debugging';
  if (/think.*through|step.by.step|analyze|compare|pros.*cons|trade.?off/.test(lower)) {
    return 'reasoning';
  }
  if (/calculate|math|equation|proof|probability|statistic/.test(lower)) return 'math';
  if (/translate|translation|in (spanish|french|german|japanese|chinese|korean)/.test(lower)) {
    return 'translation';
  }
  if (/brainstorm|creative|story|poem|write.*about|imagine/.test(lower)) return 'creative';

  if (input.length < 80) return 'simple';

  return 'general';
}

export function taskTypeToHint(taskType: TaskType): keyof ModelDefaults {
  if (taskType === 'code' || taskType === 'code_review' || taskType === 'debugging')
    return 'coding';
  if (taskType === 'reasoning' || taskType === 'math') return 'reasoning';
  if (taskType === 'translation' || taskType === 'creative') return 'chat';
  if (taskType === 'simple') return 'fast';
  return 'chat';
}

export function matchesHint(modelName: string, hint: keyof ModelDefaults): boolean {
  const n = modelName.toLowerCase();
  if (hint === 'coding') return /coder|code|deepseek|starcoder|codestral/.test(n);
  if (hint === 'reasoning') return /r1|reason|think|deepseek/.test(n);
  if (hint === 'vision') return /llava|vision|vl|moondream/.test(n);
  if (hint === 'fast') return /mini|tiny|1b|3b|7b/.test(n);
  if (hint === 'chat') return /llama|mistral|qwen|chat/.test(n);
  return false;
}

export function resolveModel(
  taskType: TaskType,
  defaults: ModelDefaults,
  loadedModels: string[],
  installedModels: string[]
): { model: string; reason: string; loaded: boolean } {
  const hint = taskTypeToHint(taskType);
  const preferred = defaults[hint];

  if (preferred && loadedModels.includes(preferred)) {
    return { model: preferred, reason: `${taskType} detected`, loaded: true };
  }

  const loadedMatch = loadedModels.find(m => matchesHint(m, hint));
  if (loadedMatch) {
    return {
      model: loadedMatch,
      reason: `${taskType} detected (using loaded model)`,
      loaded: true,
    };
  }

  if (preferred && installedModels.includes(preferred)) {
    return { model: preferred, reason: `${taskType} detected (will load model)`, loaded: false };
  }

  if (loadedModels.length > 0) {
    return { model: loadedModels[0], reason: 'Using currently loaded model', loaded: true };
  }

  return {
    model: installedModels[0] || '',
    reason: installedModels.length > 0 ? 'Using first available model' : 'No model available',
    loaded: false,
  };
}

export function autoDetectDefaults(models: string[]): ModelDefaults {
  const pick = (hint: keyof ModelDefaults): string => {
    return models.find(m => matchesHint(m, hint)) || models[0] || '';
  };

  return {
    fast: pick('fast'),
    coding: pick('coding'),
    reasoning: pick('reasoning'),
    chat: pick('chat'),
    vision: pick('vision'),
  };
}
