import request from './request';

// 登录状态分析结果
export interface LoginStatusAnalysis {
  isLoggedIn: boolean;
  hasVerification: boolean;
  verificationType?: 'captcha' | 'sms' | 'qrcode' | 'face' | 'slider' | 'other' | null;
  verificationDescription?: string | null;
  pageDescription?: string;
  suggestedAction?: string | null;
}

// 账号信息提取结果
export interface AccountInfoExtraction {
  found: boolean;
  accountName?: string | null;
  accountId?: string | null;
  avatarDescription?: string | null;
  fansCount?: number | null;
  worksCount?: number | null;
  otherInfo?: string | null;
  navigationGuide?: string | null;
}

// 页面操作指导结果
export interface PageOperationGuide {
  hasAction: boolean;
  actionType?: 'click' | 'input' | 'scroll' | 'wait' | 'navigate' | null;
  targetDescription?: string | null;
  targetSelector?: string | null;
  targetPosition?: { x: number; y: number } | null;
  inputText?: string | null;
  explanation: string;
}

// 发布状态分析结果
export interface PublishStatusAnalysis {
  status: 'uploading' | 'processing' | 'success' | 'failed' | 'need_captcha' | 'need_action';
  captchaType?: 'image' | 'sms' | 'slider' | 'other' | null;
  captchaDescription?: string | null;
  errorMessage?: string | null;
  nextAction?: {
    actionType: 'click' | 'input' | 'wait';
    targetDescription: string;
    targetSelector?: string | null;
  } | null;
  pageDescription: string;
  confidence: number;
}

export const aiApi = {
  // 检查 AI 服务状态
  getStatus(): Promise<{
    available: boolean;
    models: Record<string, string> | null;
  }> {
    return request.get('/api/ai/status');
  },

  // 分析登录状态（用于浏览器登录辅助）
  analyzeLoginStatus(imageBase64: string, platform: string): Promise<LoginStatusAnalysis> {
    return request.post('/api/ai/vision/login-status', {
      imageBase64,
      platform,
    }, { timeout: 60000 }); // 60秒超时
  },

  // 从截图中提取账号信息
  extractAccountInfo(imageBase64: string, platform: string): Promise<AccountInfoExtraction> {
    return request.post('/api/ai/vision/extract-account', {
      imageBase64,
      platform,
    }, { timeout: 60000 });
  },

  // 获取页面操作指导（AI 指导自动操作 - 基于截图）
  getOperationGuide(imageBase64: string, platform: string, goal: string): Promise<PageOperationGuide> {
    return request.post('/api/ai/vision/operation-guide', {
      imageBase64,
      platform,
      goal,
    }, { timeout: 60000 });
  },

  // 获取页面操作指导（AI 指导自动操作 - 基于 HTML，更精确）
  getHtmlOperationGuide(html: string, platform: string, goal: string): Promise<PageOperationGuide> {
    return request.post('/api/ai/html/operation-guide', {
      html,
      platform,
      goal,
    }, { timeout: 90000 }); // HTML 分析可能需要更长时间
  },

  // 分析发布页面状态（检测验证码、发布结果等）
  analyzePublishStatus(imageBase64: string, platform: string): Promise<PublishStatusAnalysis> {
    return request.post('/api/ai/publish/analyze-status', {
      imageBase64,
      platform,
    }, { timeout: 60000 });
  },

  // 分析发布页面 HTML 获取操作指导
  getPublishOperationGuide(html: string, platform: string, currentStatus: string): Promise<PageOperationGuide> {
    return request.post('/api/ai/publish/operation-guide', {
      html,
      platform,
      currentStatus,
    }, { timeout: 90000 });
  },
};
