import type { AIChatReadinessIssue } from '../components/ai/aiChatReadiness';
import { formatAIChatProviderIssueLabels } from '../components/ai/aiChatReadiness';

export type AIComposerNoticeTone = 'warning' | 'error';
export type AIComposerNoticeAction = 'open-settings' | 'reload-models';

export interface AIComposerNotice {
  tone: AIComposerNoticeTone;
  title: string;
  description: string;
  action?: {
    key: AIComposerNoticeAction;
    label: string;
  };
}

const defaultModelFetchFailedDescription = '请检查供应商入口、API Key 或账号权限，然后重新打开模型下拉。';

export const buildMissingProviderNotice = (): AIComposerNotice => ({
  tone: 'warning',
  title: '还没有可用供应商',
  description: '先在 AI 设置里添加并启用一个模型供应商。',
  action: {
    key: 'open-settings',
    label: '打开 AI 设置',
  },
});

export const buildMissingModelNotice = (): AIComposerNotice => ({
  tone: 'warning',
  title: '先选择一个模型',
  description: '打开下方模型下拉并选择模型；如果列表为空，请检查供应商入口和 API Key。',
  action: {
    key: 'reload-models',
    label: '重新加载模型',
  },
});

export const buildIncompleteProviderNotice = (issues: AIChatReadinessIssue[] = []): AIComposerNotice => {
  const missingLabels = formatAIChatProviderIssueLabels(issues.filter((issue) => issue !== 'missing_selected_model'));
  const title = missingLabels.length > 0
    ? `当前供应商还缺少 ${missingLabels.join('、')}`
    : '当前供应商配置还不完整';
  return {
    tone: 'error',
    title,
    description: '先补全供应商配置再发送，避免请求刚发起就失败。',
    action: {
      key: 'open-settings',
      label: '修复供应商配置',
    },
  };
};

export const buildModelFetchFailedNotice = (error?: string): AIComposerNotice => ({
  tone: 'error',
  title: '模型列表加载失败',
  description: String(error || '').trim() || defaultModelFetchFailedDescription,
  action: {
    key: 'reload-models',
    label: '重新加载模型',
  },
});
