import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { homedir } from 'node:os';
import type { ProviderModel } from './provider-types.js';

const QWEN_SETTINGS_PATH = join(homedir(), '.qwen', 'settings.json');

export async function readQwenSettingsModels(): Promise<ProviderModel[]> {
  try {
    const raw = await readFile(QWEN_SETTINGS_PATH, 'utf8');
    if (!raw.trim()) return [];
    const settings = JSON.parse(raw) as {
      modelProviders?: { openai?: Array<{ id: string }> };
    };
    const openaiModels = settings?.modelProviders?.openai;
    if (!Array.isArray(openaiModels)) return [];
    return openaiModels.map((m) => ({ id: m.id, label: m.id }));
  } catch {
    return [];
  }
}
