import type { ModelVerificationData } from '#/api/ai/llm';

export type VerifyButtonStatus =
  | 'error'
  | 'idle'
  | 'loading'
  | 'success'
  | 'warning';

export interface ModelVerificationFeedback {
  dimension?: number;
  message: string;
  status: 'error' | 'success' | 'warning';
}

interface ModelVerificationResponse {
  data?: ModelVerificationData;
  errorCode: number;
  message?: string;
}

const STREAMING_UNAVAILABLE_MESSAGE =
  '连接成功，流式响应不可用，可关闭智能体的模型流式响应。';

export function resolveModelVerificationFeedback(
  response: ModelVerificationResponse,
  modelType: string,
): ModelVerificationFeedback {
  if (response.errorCode !== 0) {
    return {
      message: response.message || '验证失败',
      status: 'error',
    };
  }

  if (response.data?.status === 'PARTIAL') {
    return {
      message: response.data.message || STREAMING_UNAVAILABLE_MESSAGE,
      status: 'warning',
    };
  }

  if (response.data?.status === 'FAILED') {
    return {
      message: response.data.message || '验证失败',
      status: 'error',
    };
  }

  const dimension = response.data?.dimension;
  return {
    dimension,
    message:
      modelType === 'embeddingModel' && dimension
        ? `验证成功，向量维度：${dimension}`
        : response.data?.message || '验证成功',
    status: 'success',
  };
}

export function getVerifyButtonText(status: VerifyButtonStatus): string {
  if (status === 'loading') {
    return '验证中';
  }
  if (status === 'success') {
    return '验证成功';
  }
  if (status === 'warning') {
    return '流式不可用';
  }
  if (status === 'error') {
    return '验证失败';
  }
  return '验证配置';
}
