import { AppDataSource, PlatformAccount, AccountGroup } from '../models/index.js';
import { AppError } from '../middleware/error.js';
import { ERROR_CODES, HTTP_STATUS } from '@media-manager/shared';
import type {
  PlatformType,
  PlatformAccount as PlatformAccountType,
  AccountGroup as AccountGroupType,
  AddPlatformAccountRequest,
  UpdatePlatformAccountRequest,
  CreateAccountGroupRequest,
  QRCodeInfo,
  LoginStatusResult,
} from '@media-manager/shared';
import { wsManager } from '../websocket/index.js';
import { WS_EVENTS } from '@media-manager/shared';
import { CookieManager } from '../automation/cookie.js';
import { logger } from '../utils/logger.js';
import { headlessBrowserService } from './HeadlessBrowserService.js';
import { aiService } from '../ai/index.js';
import { UserDayStatisticsService } from './UserDayStatisticsService.js';

interface GetAccountsParams {
  platform?: string;
  groupId?: number;
  status?: string;
}

export class AccountService {
  private accountRepository = AppDataSource.getRepository(PlatformAccount);
  private groupRepository = AppDataSource.getRepository(AccountGroup);

  // ============ 账号分组 ============

  async getGroups(userId: number): Promise<AccountGroupType[]> {
    const groups = await this.groupRepository.find({
      where: { userId },
      order: { createdAt: 'ASC' },
    });
    return groups.map(this.formatGroup);
  }

  async createGroup(userId: number, data: CreateAccountGroupRequest): Promise<AccountGroupType> {
    const group = this.groupRepository.create({
      userId,
      name: data.name,
      description: data.description || null,
    });
    await this.groupRepository.save(group);
    return this.formatGroup(group);
  }

  async updateGroup(
    userId: number,
    groupId: number,
    data: Partial<CreateAccountGroupRequest>
  ): Promise<AccountGroupType> {
    const group = await this.groupRepository.findOne({
      where: { id: groupId, userId },
    });
    if (!group) {
      throw new AppError('分组不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.NOT_FOUND);
    }

    await this.groupRepository.update(groupId, {
      name: data.name ?? group.name,
      description: data.description ?? group.description,
    });

    const updated = await this.groupRepository.findOne({ where: { id: groupId } });
    return this.formatGroup(updated!);
  }

  async deleteGroup(userId: number, groupId: number): Promise<void> {
    const group = await this.groupRepository.findOne({
      where: { id: groupId, userId },
    });
    if (!group) {
      throw new AppError('分组不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.NOT_FOUND);
    }
    await this.groupRepository.delete(groupId);
  }

  // ============ 平台账号 ============

  async getAccounts(userId: number, params: GetAccountsParams): Promise<PlatformAccountType[]> {
    const queryBuilder = this.accountRepository
      .createQueryBuilder('account')
      .where('account.userId = :userId', { userId });

    if (params.platform) {
      queryBuilder.andWhere('account.platform = :platform', { platform: params.platform });
    }
    if (params.groupId) {
      queryBuilder.andWhere('account.groupId = :groupId', { groupId: params.groupId });
    }
    if (params.status) {
      queryBuilder.andWhere('account.status = :status', { status: params.status });
    }

    const accounts = await queryBuilder
      .orderBy('account.createdAt', 'DESC')
      .getMany();

    return accounts.map(this.formatAccount);
  }

  async getAccountById(userId: number, accountId: number): Promise<PlatformAccountType> {
    const account = await this.accountRepository.findOne({
      where: { id: accountId, userId },
    });
    if (!account) {
      throw new AppError('账号不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.ACCOUNT_NOT_FOUND);
    }
    return this.formatAccount(account);
  }

  /**
   * 获取账号的 Cookie 数据（用于客户端打开后台）
   */
  async getAccountCookie(userId: number, accountId: number): Promise<string> {
    const account = await this.accountRepository.findOne({
      where: { id: accountId, userId },
    });
    if (!account) {
      throw new AppError('账号不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.ACCOUNT_NOT_FOUND);
    }
    
    if (!account.cookieData) {
      throw new AppError('账号没有 Cookie 数据', HTTP_STATUS.BAD_REQUEST, ERROR_CODES.VALIDATION_ERROR);
    }
    
    // 尝试解密 Cookie
    let decryptedCookies: string;
    try {
      decryptedCookies = CookieManager.decrypt(account.cookieData);
      logger.info(`[AccountService] Cookie 解密成功，账号: ${account.accountName}`);
    } catch (error) {
      // 如果解密失败，返回原始数据
      logger.warn(`[AccountService] Cookie 解密失败，使用原始数据，账号: ${account.accountName}`);
      decryptedCookies = account.cookieData;
    }
    
    // 验证 Cookie 格式
    try {
      const parsed = JSON.parse(decryptedCookies);
      if (Array.isArray(parsed) && parsed.length > 0) {
        logger.info(`[AccountService] Cookie 格式验证通过，共 ${parsed.length} 个 Cookie`);
        // 记录关键 Cookie（用于调试）
        const keyNames = parsed.slice(0, 3).map((c: any) => c.name).join(', ');
        logger.info(`[AccountService] 关键 Cookie: ${keyNames}`);
      }
    } catch {
      logger.warn(`[AccountService] Cookie 不是 JSON 格式，可能是字符串格式`);
    }
    
    return decryptedCookies;
  }

  async addAccount(userId: number, data: AddPlatformAccountRequest & {
    accountInfo?: {
      accountId?: string;
      accountName?: string;
      avatarUrl?: string;
      fansCount?: number;
      worksCount?: number;
    };
  }): Promise<PlatformAccountType> {
    const platform = data.platform as PlatformType;
    
    // 解密 Cookie（如果是加密的）
    let cookieData = data.cookieData;
    let decryptedCookies: string;
    
    try {
      // 尝试解密（如果是通过浏览器登录获取的加密Cookie）
      decryptedCookies = CookieManager.decrypt(cookieData);
    } catch {
      // 如果解密失败，可能是直接粘贴的Cookie字符串
      decryptedCookies = cookieData;
    }
    
    // 检查客户端传入的 accountId 是否有效（不是纯时间戳）
    const clientAccountId = data.accountInfo?.accountId;
    const isValidClientAccountId = clientAccountId && !this.isTimestampBasedId(clientAccountId);
    
    // 从 Cookie 中提取账号 ID（部分平台的 Cookie 包含真实用户 ID）
    const accountIdFromCookie = this.extractAccountIdFromCookie(platform, decryptedCookies);
    
    // 某些平台应优先使用 API 返回的真实 ID，而不是 Cookie 中的值
    // - 抖音：使用抖音号（unique_id，如 Ethanfly9392），而不是 Cookie 中的 passport_uid
    // - 小红书：使用小红书号（red_num），而不是 Cookie 中的 userid
    // - 百家号：使用 new_uc_id，而不是 Cookie 中的 BDUSS
    // - 视频号/头条：使用 API 返回的真实账号 ID
    const platformsPreferApiId: PlatformType[] = ['douyin', 'xiaohongshu', 'baijiahao', 'weixin_video', 'toutiao'];
    const preferApiId = platformsPreferApiId.includes(platform);
    
    // 确定最终的 accountId
    let finalAccountId: string;
    
    if (preferApiId && isValidClientAccountId) {
      // 对于优先使用 API ID 的平台，先用客户端传入的有效 ID
      finalAccountId = this.normalizeAccountId(platform, clientAccountId);
      logger.info(`[addAccount] Using API-based accountId for ${platform}: ${finalAccountId}`);
    } else if (accountIdFromCookie) {
      // 其他平台优先使用 Cookie 中提取的 ID
      finalAccountId = accountIdFromCookie;
      logger.info(`[addAccount] Using accountId from cookie: ${finalAccountId}`);
    } else if (isValidClientAccountId) {
      // 再次尝试客户端 ID
      finalAccountId = this.normalizeAccountId(platform, clientAccountId);
      logger.info(`[addAccount] Using valid client accountId: ${finalAccountId}`);
    } else {
      finalAccountId = `${platform}_${Date.now()}`;
      logger.warn(`[addAccount] Using timestamp-based accountId as fallback: ${finalAccountId}`);
    }
    
    // 使用传入的账号信息（来自浏览器登录会话），或使用默认值
    const accountInfo = {
      accountId: finalAccountId,
      accountName: data.accountInfo?.accountName || `${platform}账号`,
      avatarUrl: data.accountInfo?.avatarUrl || null,
      fansCount: data.accountInfo?.fansCount || 0,
      worksCount: data.accountInfo?.worksCount || 0,
    };
    
    logger.info(`Adding account for ${platform}: ${accountInfo.accountId}, name: ${accountInfo.accountName}`);

    // 检查是否已存在相同账号
    const existing = await this.accountRepository.findOne({
      where: { userId, platform, accountId: accountInfo.accountId },
    });
    
    if (existing) {
      // 更新已存在的账号
      await this.accountRepository.update(existing.id, {
        cookieData: cookieData, // 存储原始数据（可能是加密的）
        accountName: accountInfo.accountName,
        avatarUrl: accountInfo.avatarUrl,
        fansCount: accountInfo.fansCount,
        worksCount: accountInfo.worksCount,
        status: 'active',
        groupId: data.groupId || existing.groupId,
        proxyConfig: data.proxyConfig || existing.proxyConfig,
      });
      
      const updated = await this.accountRepository.findOne({ where: { id: existing.id } });
      wsManager.sendToUser(userId, WS_EVENTS.ACCOUNT_UPDATED, { account: this.formatAccount(updated!) });
      
      // 异步刷新账号信息（获取准确的粉丝数、作品数等）
      this.refreshAccountAsync(userId, existing.id, platform).catch(err => {
        logger.warn(`[addAccount] Background refresh failed for existing account ${existing.id}:`, err);
      });
      
      return this.formatAccount(updated!);
    }

    // 创建新账号
    const account = this.accountRepository.create({
      userId,
      platform,
      cookieData: cookieData,
      groupId: data.groupId || null,
      proxyConfig: data.proxyConfig || null,
      status: 'active',
      accountId: accountInfo.accountId,
      accountName: accountInfo.accountName,
      avatarUrl: accountInfo.avatarUrl,
      fansCount: accountInfo.fansCount,
      worksCount: accountInfo.worksCount,
    });

    await this.accountRepository.save(account);

    // 通知其他客户端
    wsManager.sendToUser(userId, WS_EVENTS.ACCOUNT_ADDED, { account: this.formatAccount(account) });

    // 异步刷新账号信息（获取准确的粉丝数、作品数等）
    // 不阻塞返回，后台执行
    this.refreshAccountAsync(userId, account.id, platform).catch(err => {
      logger.warn(`[addAccount] Background refresh failed for account ${account.id}:`, err);
    });

    return this.formatAccount(account);
  }

  /**
   * 异步刷新账号信息（用于添加账号后获取准确数据）
   */
  private async refreshAccountAsync(userId: number, accountId: number, platform: PlatformType): Promise<void> {
    // 延迟 2 秒执行，等待前端处理完成
    await new Promise(resolve => setTimeout(resolve, 2000));
    
    logger.info(`[addAccount] Starting background refresh for account ${accountId} (${platform})`);
    
    try {
      await this.refreshAccount(userId, accountId);
      logger.info(`[addAccount] Background refresh completed for account ${accountId}`);
    } catch (error) {
      logger.warn(`[addAccount] Background refresh failed for account ${accountId}:`, error);
    }
  }

  async updateAccount(
    userId: number,
    accountId: number,
    data: UpdatePlatformAccountRequest
  ): Promise<PlatformAccountType> {
    const account = await this.accountRepository.findOne({
      where: { id: accountId, userId },
    });
    if (!account) {
      throw new AppError('账号不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.ACCOUNT_NOT_FOUND);
    }

    await this.accountRepository.update(accountId, {
      groupId: data.groupId !== undefined ? data.groupId : account.groupId,
      proxyConfig: data.proxyConfig !== undefined ? data.proxyConfig : account.proxyConfig,
      status: data.status ?? account.status,
    });

    const updated = await this.accountRepository.findOne({ where: { id: accountId } });

    // 通知其他客户端
    wsManager.sendToUser(userId, WS_EVENTS.ACCOUNT_UPDATED, { account: this.formatAccount(updated!) });

    return this.formatAccount(updated!);
  }

  async deleteAccount(userId: number, accountId: number): Promise<void> {
    const account = await this.accountRepository.findOne({
      where: { id: accountId, userId },
    });
    if (!account) {
      throw new AppError('账号不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.ACCOUNT_NOT_FOUND);
    }
    await this.accountRepository.delete(accountId);

    // 通知其他客户端
    wsManager.sendToUser(userId, WS_EVENTS.ACCOUNT_DELETED, { accountId });
  }

  async refreshAccount(userId: number, accountId: number): Promise<PlatformAccountType & { needReLogin?: boolean }> {
    const account = await this.accountRepository.findOne({
      where: { id: accountId, userId },
    });
    if (!account) {
      throw new AppError('账号不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.ACCOUNT_NOT_FOUND);
    }

    const platform = account.platform as PlatformType;
    const updateData: Partial<PlatformAccount> = {
      updatedAt: new Date(),
    };

    let needReLogin = false;
    let aiRefreshSuccess = false;

    // 尝试通过无头浏览器刷新账号信息
    if (account.cookieData) {
      try {
        // 解密 Cookie
        let decryptedCookies: string;
        try {
          decryptedCookies = CookieManager.decrypt(account.cookieData);
        } catch {
          decryptedCookies = account.cookieData;
        }

        // 解析 Cookie - 支持两种格式
        let cookieList: { name: string; value: string; domain: string; path: string }[];
        let cookieParseError = false;
        try {
          // 先尝试 JSON 格式
          cookieList = JSON.parse(decryptedCookies);
        } catch {
          // 如果 JSON 解析失败，尝试解析 "name=value; name2=value2" 格式
          cookieList = this.parseCookieString(decryptedCookies, platform);
          if (cookieList.length === 0) {
            logger.error(`Invalid cookie format for account ${accountId}`);
            cookieParseError = true;
          }
        }

        if (cookieList.length > 0 && !cookieParseError) {
          // 抖音、小红书、百家号直接使用 API 获取准确数据，不使用 AI（因为 AI 可能识别错误）
          const platformsSkipAI: PlatformType[] = ['douyin', 'xiaohongshu', 'baijiahao'];
          const shouldUseAI = aiService.isAvailable() && !platformsSkipAI.includes(platform);
          
          logger.info(`[refreshAccount] Platform: ${platform}, shouldUseAI: ${shouldUseAI}, aiAvailable: ${aiService.isAvailable()}`);
          
          // ========== AI 辅助刷新（部分平台使用） ==========
          if (shouldUseAI) {
            try {
              logger.info(`[AI Refresh] Starting AI-assisted refresh for account ${accountId} (${platform})`);
              
              // 使用无头浏览器截图，然后 AI 分析
              const aiResult = await this.refreshAccountWithAI(platform, cookieList, accountId);
              
              if (aiResult.needReLogin) {
                // AI 检测到需要重新登录
                updateData.status = 'expired';
                needReLogin = true;
                aiRefreshSuccess = true;
                logger.warn(`[AI Refresh] Account ${accountId} needs re-login (detected by AI)`);
              } else if (aiResult.accountInfo) {
                // AI 成功获取到账号信息
                updateData.status = 'active';
                updateData.accountName = aiResult.accountInfo.accountName;
                if (aiResult.accountInfo.avatarUrl) {
                  updateData.avatarUrl = aiResult.accountInfo.avatarUrl;
                }
                if (aiResult.accountInfo.fansCount !== undefined) {
                  updateData.fansCount = aiResult.accountInfo.fansCount;
                }
                if (aiResult.accountInfo.worksCount !== undefined) {
                  updateData.worksCount = aiResult.accountInfo.worksCount;
                }
                aiRefreshSuccess = true;
                logger.info(`[AI Refresh] Successfully refreshed account ${accountId}: ${aiResult.accountInfo.accountName}`);
              }
            } catch (aiError) {
              logger.warn(`[AI Refresh] AI-assisted refresh failed for account ${accountId}:`, aiError);
              // AI 刷新失败，继续使用原有逻辑
            }
          }
          
          // ========== 原有逻辑（AI 失败时的备用方案） ==========
          if (!aiRefreshSuccess) {
            // 第一步：通过浏览器检查 Cookie 是否有效
            const isValid = await headlessBrowserService.checkCookieValid(platform, cookieList);
            
            if (!isValid) {
              // Cookie 已过期，需要重新登录
              updateData.status = 'expired';
              needReLogin = true;
              logger.warn(`Account ${accountId} (${account.accountName}) cookie expired, need re-login`);
            } else {
              // Cookie 有效，尝试获取账号信息
              updateData.status = 'active';
              
              try {
                const profile = await headlessBrowserService.fetchAccountInfo(platform, cookieList);

                // 检查是否获取到有效信息（排除默认名称）
                const defaultNames = [
                  `${platform}账号`, '未知账号', '抖音账号', '小红书账号', 
                  '快手账号', '视频号账号', 'B站账号', '头条账号', '百家号账号'
                ];
                const isValidProfile = profile.accountName && !defaultNames.includes(profile.accountName);

                if (isValidProfile) {
                  updateData.accountName = profile.accountName;
                  updateData.avatarUrl = profile.avatarUrl;
                  updateData.fansCount = profile.fansCount;
                  updateData.worksCount = profile.worksCount;
                  
                  // 如果获取到了有效的 accountId（如抖音号），也更新它
                  // 这样可以修正之前使用错误 ID（如 Cookie 值）保存的账号
                  if (profile.accountId && !this.isTimestampBasedId(profile.accountId)) {
                    const newAccountId = this.normalizeAccountId(platform, profile.accountId);
                    // 只有当新 ID 与旧 ID 不同时才更新
                    if (newAccountId !== account.accountId) {
                      updateData.accountId = newAccountId;
                      logger.info(`[refreshAccount] Updating accountId from ${account.accountId} to ${newAccountId}`);
                    }
                  }
                  
                  logger.info(`Refreshed account info for ${platform}: ${profile.accountName}, fans: ${profile.fansCount}, works: ${profile.worksCount}`);
                } else {
                  // 获取的信息无效，但 Cookie 有效，保持 active 状态
                  logger.warn(`Could not fetch valid account info for ${accountId}, but cookie is valid`);
                }
              } catch (infoError) {
                // 获取账号信息失败，但 Cookie 检查已通过，保持 active 状态
                logger.warn(`Failed to fetch account info for ${accountId}, but cookie is valid:`, infoError);
              }
            }
          }
        }
        // Cookie 解析失败时，不改变状态
      } catch (error) {
        logger.error(`Failed to refresh account ${accountId}:`, error);
        // 不抛出错误，不改变状态，只更新时间戳
      }
    }
    // 没有 Cookie 数据时，不改变状态

    await this.accountRepository.update(accountId, updateData);

    const updated = await this.accountRepository.findOne({ where: { id: accountId } });

    // 保存账号每日统计数据（粉丝数、作品数）
    // 无论是否更新了粉丝数/作品数，都要保存当前值到统计表，确保每天都有记录
    // TODO: 暂时注释掉，待后续需要时再启用
    // if (updated) {
    //   try {
    //     const userDayStatisticsService = new UserDayStatisticsService();
    //     await userDayStatisticsService.saveStatistics({
    //       accountId,
    //       fansCount: updated.fansCount || 0,
    //       worksCount: updated.worksCount || 0,
    //     });
    //     logger.debug(`[AccountService] Saved account day statistics for account ${accountId} (fans: ${updated.fansCount || 0}, works: ${updated.worksCount || 0})`);
    //   } catch (error) {
    //     logger.error(`[AccountService] Failed to save account day statistics for account ${accountId}:`, error);
    //     // 不抛出错误，不影响主流程
    //   }
    // }

    // 通知其他客户端
    wsManager.sendToUser(userId, WS_EVENTS.ACCOUNT_UPDATED, { account: this.formatAccount(updated!) });

    return { ...this.formatAccount(updated!), needReLogin };
  }

  /**
   * 使用 AI 辅助刷新账号信息
   */
  private async refreshAccountWithAI(
    platform: PlatformType,
    cookieList: { name: string; value: string; domain: string; path: string }[],
    accountId: number
  ): Promise<{
    needReLogin: boolean;
    accountInfo?: {
      accountName: string;
      avatarUrl?: string;
      fansCount?: number;
      worksCount?: number;
    };
  }> {
    // 使用无头浏览器访问平台后台并截图
    const screenshot = await headlessBrowserService.capturePageScreenshot(platform, cookieList);
    
    if (!screenshot) {
      throw new Error('Failed to capture screenshot');
    }
    
    // 第一步：使用 AI 分析登录状态
    const loginStatus = await aiService.analyzeLoginStatus(screenshot, platform);
    
    logger.info(`[AI Refresh] Login status for account ${accountId}:`, {
      isLoggedIn: loginStatus.isLoggedIn,
      hasVerification: loginStatus.hasVerification,
    });
    
    // 如果 AI 检测到未登录或有验证码，说明需要重新登录
    if (!loginStatus.isLoggedIn || loginStatus.hasVerification) {
      return { needReLogin: true };
    }
    
    // 第二步：使用 AI 提取账号信息
    const accountInfo = await aiService.extractAccountInfo(screenshot, platform);
    
    logger.info(`[AI Refresh] Account info extraction for ${accountId}:`, {
      found: accountInfo.found,
      accountName: accountInfo.accountName,
    });
    
    if (accountInfo.found && accountInfo.accountName) {
      return {
        needReLogin: false,
        accountInfo: {
          accountName: accountInfo.accountName,
          fansCount: accountInfo.fansCount ? parseInt(String(accountInfo.fansCount)) : undefined,
          worksCount: accountInfo.worksCount ? parseInt(String(accountInfo.worksCount)) : undefined,
        },
      };
    }
    
    // AI 未能提取到账号信息，但登录状态正常
    // 返回空结果，让原有逻辑处理
    return { needReLogin: false };
  }

  /**
   * 检查账号 Cookie 是否有效
   */
  async checkAccountStatus(userId: number, accountId: number): Promise<{ isValid: boolean }> {
    const account = await this.accountRepository.findOne({
      where: { id: accountId, userId },
    });
    if (!account) {
      throw new AppError('账号不存在', HTTP_STATUS.NOT_FOUND, ERROR_CODES.ACCOUNT_NOT_FOUND);
    }

    if (!account.cookieData) {
      // 更新状态为过期
      await this.accountRepository.update(accountId, { status: 'expired' });
      return { isValid: false };
    }

    const platform = account.platform as PlatformType;

    try {
      // 解密 Cookie
      let decryptedCookies: string;
      try {
        decryptedCookies = CookieManager.decrypt(account.cookieData);
      } catch {
        decryptedCookies = account.cookieData;
      }

      // 解析 Cookie - 支持两种格式
      let cookieList: { name: string; value: string; domain: string; path: string }[];
      try {
        // 先尝试 JSON 格式
        cookieList = JSON.parse(decryptedCookies);
      } catch {
        // 如果 JSON 解析失败，尝试解析 "name=value; name2=value2" 格式
        cookieList = this.parseCookieString(decryptedCookies, platform);
        if (cookieList.length === 0) {
          await this.accountRepository.update(accountId, { status: 'expired' });
          return { isValid: false };
        }
      }

      // 使用 API 检查 Cookie 是否有效
      const isValid = await headlessBrowserService.checkCookieValid(platform, cookieList);

      // 更新账号状态
      if (!isValid) {
        await this.accountRepository.update(accountId, { status: 'expired' });
        wsManager.sendToUser(userId, WS_EVENTS.ACCOUNT_UPDATED, { 
          account: { ...this.formatAccount(account), status: 'expired' }
        });
      } else if (account.status === 'expired') {
        // 如果之前是过期状态但现在有效了，更新为正常
        await this.accountRepository.update(accountId, { status: 'active' });
      }

      return { isValid };
    } catch (error) {
      logger.error(`Failed to check account status ${accountId}:`, error);
      return { isValid: false };
    }
  }

  /**
   * 批量刷新所有账号状态
   */
  async refreshAllAccounts(userId: number): Promise<{ refreshed: number; failed: number }> {
    const accounts = await this.accountRepository.find({
      where: { userId },
    });

    let refreshed = 0;
    let failed = 0;

    for (const account of accounts) {
      try {
        await this.refreshAccount(userId, account.id);
        refreshed++;
      } catch (error) {
        logger.error(`Failed to refresh account ${account.id}:`, error);
        failed++;
      }
    }

    logger.info(`Refreshed ${refreshed} accounts for user ${userId}, ${failed} failed`);
    return { refreshed, failed };
  }

  async getQRCode(platform: string): Promise<QRCodeInfo> {
    // TODO: 调用对应平台适配器获取二维码
    // 这里返回模拟数据
    return {
      qrcodeUrl: `https://example.com/qrcode/${platform}/${Date.now()}`,
      qrcodeKey: `qr_${platform}_${Date.now()}`,
      expireTime: Date.now() + 300000, // 5分钟后过期
    };
  }

  async checkQRCodeStatus(platform: string, qrcodeKey: string): Promise<LoginStatusResult> {
    // TODO: 调用对应平台适配器检查扫码状态
    // 这里返回模拟数据
    return {
      status: 'waiting',
      message: '等待扫码',
    };
  }

  /**
   * 验证 Cookie 并获取账号信息（用于 Electron 内嵌浏览器登录）
   */
  async verifyCookieAndGetInfo(platform: PlatformType, cookieData: string): Promise<{
    success: boolean;
    message?: string;
    accountInfo?: {
      accountId: string;
      accountName: string;
      avatarUrl: string;
      fansCount: number;
      worksCount: number;
    };
  }> {
    try {
      // 将 cookie 字符串转换为 cookie 列表格式
      const cookieList = this.parseCookieString(cookieData, platform);
      
      if (cookieList.length === 0) {
        return { success: false, message: 'Cookie 格式无效' };
      }

      // 先尝试获取账号信息（可以同时验证登录状态）
      // 如果能成功获取到有效信息，说明登录是有效的
      try {
        const profile = await headlessBrowserService.fetchAccountInfo(platform, cookieList);
        
        // 检查是否获取到有效信息（排除默认名称）
        const defaultNames = [
          `${platform}账号`, '未知账号', '抖音账号', '小红书账号', 
          '快手账号', '视频号账号', 'B站账号', '头条账号', '百家号账号'
        ];
        const isValidProfile = profile.accountName && !defaultNames.includes(profile.accountName);

        if (isValidProfile) {
          return {
            success: true,
            accountInfo: {
              // 确保 accountId 带有平台前缀
              accountId: this.normalizeAccountId(platform, profile.accountId || ''),
              accountName: profile.accountName,
              avatarUrl: profile.avatarUrl || '',
              fansCount: profile.fansCount || 0,
              worksCount: profile.worksCount || 0,
            },
          };
        }
        
        // 未能获取有效信息，再验证 Cookie 是否有效
        logger.info(`[verifyCookieAndGetInfo] Could not get valid profile for ${platform}, checking cookie validity...`);
      } catch (infoError) {
        logger.warn(`Failed to fetch account info for ${platform}:`, infoError);
      }
      
      // 账号信息获取失败或无效，检查 Cookie 是否有效
      const isValid = await headlessBrowserService.checkCookieValid(platform, cookieList);
      
      if (isValid) {
        // Cookie 有效但未能获取账号信息，返回基本成功
        return {
          success: true,
          message: '登录成功，但无法获取详细信息',
          accountInfo: {
            accountId: `${platform}_${Date.now()}`,
            accountName: `${platform}账号`,
            avatarUrl: '',
            fansCount: 0,
            worksCount: 0,
          },
        };
      } else {
        // Cookie 无效
        return { success: false, message: '登录状态无效或已过期' };
      }
    } catch (error) {
      logger.error(`Failed to verify cookie for ${platform}:`, error);
      return { 
        success: false, 
        message: error instanceof Error ? error.message : '验证失败' 
      };
    }
  }

  /**
   * 将 cookie 字符串解析为 cookie 列表
   */
  private parseCookieString(cookieString: string, platform: PlatformType): { 
    name: string; 
    value: string; 
    domain: string; 
    path: string;
  }[] {
    // 获取平台对应的域名
    const domainMap: Record<string, string> = {
      douyin: '.douyin.com',
      kuaishou: '.kuaishou.com',
      xiaohongshu: '.xiaohongshu.com',
      weixin_video: '.qq.com',
      bilibili: '.bilibili.com',
      toutiao: '.toutiao.com',
      baijiahao: '.baidu.com',
      qie: '.qq.com',
      dayuhao: '.alibaba.com',
    };
    
    const domain = domainMap[platform] || `.${platform}.com`;
    
    // 解析 "name=value; name2=value2" 格式的 cookie 字符串
    const cookies: { name: string; value: string; domain: string; path: string }[] = [];
    
    const pairs = cookieString.split(';');
    for (const pair of pairs) {
      const trimmed = pair.trim();
      if (!trimmed) continue;
      
      const eqIndex = trimmed.indexOf('=');
      if (eqIndex === -1) continue;
      
      const name = trimmed.substring(0, eqIndex).trim();
      const value = trimmed.substring(eqIndex + 1).trim();
      
      if (name && value) {
        cookies.push({
          name,
          value,
          domain,
          path: '/',
        });
      }
    }
    
    return cookies;
  }

  private formatGroup(group: AccountGroup): AccountGroupType {
    return {
      id: group.id,
      userId: group.userId,
      name: group.name,
      description: group.description,
      createdAt: group.createdAt.toISOString(),
    };
  }

  private formatAccount(account: PlatformAccount): PlatformAccountType {
    return {
      id: account.id,
      userId: account.userId,
      platform: account.platform,
      accountName: account.accountName || '',
      accountId: account.accountId || '',
      avatarUrl: account.avatarUrl,
      fansCount: account.fansCount,
      worksCount: account.worksCount,
      status: account.status,
      proxyConfig: account.proxyConfig,
      groupId: account.groupId,
      cookieExpireAt: account.cookieExpireAt?.toISOString() || null,
      createdAt: account.createdAt.toISOString(),
      updatedAt: account.updatedAt.toISOString(),
    };
  }

  /**
   * 从 Cookie 中提取账号 ID（最可靠的方式）
   * 不同平台使用不同的 Cookie 字段来标识用户
   */
  private extractAccountIdFromCookie(platform: PlatformType, cookieString: string): string | null {
    // 各平台用于标识用户的 Cookie 名称（按优先级排序）
    const platformCookieNames: Record<string, string[]> = {
      douyin: ['passport_uid', 'uid_tt', 'ttwid', 'sessionid_ss'],
      kuaishou: ['userId', 'passToken', 'did'],
      xiaohongshu: ['customerClientId', 'web_session', 'xsecappid'],
      weixin_video: ['wxuin', 'pass_ticket', 'uin'],
      bilibili: ['DedeUserID', 'SESSDATA', 'bili_jct'],
      toutiao: ['sso_uid', 'sessionid', 'passport_uid'],
      baijiahao: ['BDUSS', 'STOKEN', 'BAIDUID'],
      qie: ['uin', 'skey', 'p_uin'],
      dayuhao: ['login_aliyunid', 'cna', 'munb'],
    };
    
    const targetCookieNames = platformCookieNames[platform] || [];
    if (targetCookieNames.length === 0) {
      return null;
    }
    
    try {
      // 尝试解析 JSON 格式的 Cookie
      let cookieList: { name: string; value: string }[];
      try {
        cookieList = JSON.parse(cookieString);
      } catch {
        // 如果不是 JSON，尝试解析 "name=value; name2=value2" 格式
        cookieList = this.parseCookieString(cookieString, platform).map(c => ({
          name: c.name,
          value: c.value,
        }));
      }
      
      if (!Array.isArray(cookieList) || cookieList.length === 0) {
        return null;
      }
      
      // 按优先级查找 Cookie
      for (const cookieName of targetCookieNames) {
        const cookie = cookieList.find(c => c.name === cookieName);
        if (cookie?.value) {
          // 获取 Cookie 值，处理可能的编码
          let cookieValue = cookie.value;
          
          // 处理特殊格式的 Cookie（如 ttwid 可能包含分隔符）
          if (cookieValue.includes('|')) {
            cookieValue = cookieValue.split('|')[1] || cookieValue;
          }
          if (cookieValue.includes('%')) {
            try {
              cookieValue = decodeURIComponent(cookieValue);
            } catch {
              // 解码失败，使用原值
            }
          }
          
          // 截取合理长度（避免过长的 ID）
          if (cookieValue.length > 64) {
            cookieValue = cookieValue.slice(0, 64);
          }
          
          const accountId = `${platform}_${cookieValue}`;
          logger.info(`[extractAccountIdFromCookie] Found ${cookieName} for ${platform}: ${accountId}`);
          return accountId;
        }
      }
      
      return null;
    } catch (error) {
      logger.warn(`[extractAccountIdFromCookie] Failed to extract accountId from cookie for ${platform}:`, error);
      return null;
    }
  }

  /**
   * 平台短前缀映射（统一使用短前缀）
   */
  private static readonly SHORT_PREFIX_MAP: Record<PlatformType, string> = {
    'douyin': 'dy_',
    'xiaohongshu': 'xhs_',
    'weixin_video': 'sph_',
    'baijiahao': 'bjh_',
    'kuaishou': 'ks_',
    'bilibili': 'bili_',
    'toutiao': 'tt_',
    'qie': 'qie_',
    'dayuhao': 'dyh_',
  };

  /**
   * 标准化 accountId 格式，统一使用短前缀
   * 例如：Ethanfly9392 -> dy_Ethanfly9392
   */
  private normalizeAccountId(platform: PlatformType, accountId: string): string {
    const shortPrefix = AccountService.SHORT_PREFIX_MAP[platform] || `${platform}_`;
    
    if (!accountId) {
      return `${shortPrefix}${Date.now()}`;
    }
    
    // 如果已经有正确的短前缀，直接返回
    if (accountId.startsWith(shortPrefix)) {
      return accountId;
    }
    
    // 移除任何已有的前缀（短前缀或完整前缀）
    const allShortPrefixes = Object.values(AccountService.SHORT_PREFIX_MAP);
    const allFullPrefixes = Object.keys(AccountService.SHORT_PREFIX_MAP).map(p => `${p}_`);
    const allPrefixes = [...allShortPrefixes, ...allFullPrefixes];
    
    let cleanId = accountId;
    for (const prefix of allPrefixes) {
      if (cleanId.startsWith(prefix)) {
        cleanId = cleanId.slice(prefix.length);
        break;
      }
    }
    
    // 添加正确的短前缀
    return `${shortPrefix}${cleanId}`;
  }

  /**
   * 检查 accountId 是否是基于时间戳生成的（不可靠的 ID）
   * 时间戳 ID 格式通常是：dy_1737619200000 或 douyin_1737619200000
   */
  private isTimestampBasedId(accountId: string): boolean {
    if (!accountId) return true;
    
    // 检查是否匹配 前缀_时间戳 格式（支持短前缀和完整前缀）
    const timestampPattern = /^[a-z_]+_(\d{13,})$/;
    const match = accountId.match(timestampPattern);
    if (!match) {
      return false;
    }
    
    // 提取数字部分，检查是否是合理的时间戳（2020年到2030年之间）
    if (match[1]) {
      const timestamp = parseInt(match[1]);
      const minTimestamp = new Date('2020-01-01').getTime(); // 1577836800000
      const maxTimestamp = new Date('2030-01-01').getTime(); // 1893456000000
      
      if (timestamp >= minTimestamp && timestamp <= maxTimestamp) {
        logger.info(`[isTimestampBasedId] Detected timestamp-based ID: ${accountId}`);
        return true;
      }
    }
    
    return false;
  }
}
