#!/usr/bin/env tsx
/**
 * 手动测试四个平台的数据同步任务
 * 用于测试账号刷新重试机制
 * 
 * 运行: cd server && pnpm exec tsx src/scripts/test-data-sync.ts
 */
import { initDatabase } from '../models/index.js';
import { XiaohongshuAccountOverviewImportService } from '../services/XiaohongshuAccountOverviewImportService.js';
import { DouyinAccountOverviewImportService } from '../services/DouyinAccountOverviewImportService.js';
import { BaijiahaoContentOverviewImportService } from '../services/BaijiahaoContentOverviewImportService.js';
import { WeixinVideoDataCenterImportService } from '../services/WeixinVideoDataCenterImportService.js';
import { logger } from '../utils/logger.js';

async function main() {
  logger.info('========================================');
  logger.info('Starting manual data sync test...');
  logger.info('========================================');

  // 初始化数据库连接
  await initDatabase();
  logger.info('Database initialized');

  const args = process.argv.slice(2);
  const platform = args[0]?.toLowerCase();

  if (!platform || !['xhs', 'dy', 'bj', 'wx', 'all'].includes(platform)) {
    console.log('\nUsage: pnpm exec tsx src/scripts/test-data-sync.ts <platform>');
    console.log('\nPlatforms:');
    console.log('  xhs  - 小红书 (Xiaohongshu)');
    console.log('  dy   - 抖音 (Douyin)');
    console.log('  bj   - 百家号 (Baijiahao)');
    console.log('  wx   - 视频号 (Weixin Video)');
    console.log('  all  - 所有平台');
    console.log('\nExample: pnpm exec tsx src/scripts/test-data-sync.ts xhs');
    process.exit(1);
  }

  try {
    if (platform === 'xhs' || platform === 'all') {
      logger.info('\n========================================');
      logger.info('Testing Xiaohongshu (小红书) data sync...');
      logger.info('========================================');
      await XiaohongshuAccountOverviewImportService.runDailyImport();
      logger.info('✓ Xiaohongshu sync completed');
    }

    if (platform === 'dy' || platform === 'all') {
      logger.info('\n========================================');
      logger.info('Testing Douyin (抖音) data sync...');
      logger.info('========================================');
      await DouyinAccountOverviewImportService.runDailyImport();
      logger.info('✓ Douyin sync completed');
    }

    if (platform === 'bj' || platform === 'all') {
      logger.info('\n========================================');
      logger.info('Testing Baijiahao (百家号) data sync...');
      logger.info('========================================');
      await BaijiahaoContentOverviewImportService.runDailyImport();
      logger.info('✓ Baijiahao sync completed');
    }

    if (platform === 'wx' || platform === 'all') {
      logger.info('\n========================================');
      logger.info('Testing Weixin Video (视频号) data sync...');
      logger.info('========================================');
      await WeixinVideoDataCenterImportService.runDailyImport();
      logger.info('✓ Weixin Video sync completed');
    }

    logger.info('\n========================================');
    logger.info('All tests completed successfully!');
    logger.info('========================================');
  } catch (error) {
    logger.error('Test failed:', error);
    process.exit(1);
  }

  process.exit(0);
}

main();
