import { initDatabase } from '../models/index.js';
import { logger } from '../utils/logger.js';
import { BaijiahaoWorkDailyStatisticsImportService } from '../services/BaijiahaoWorkDailyStatisticsImportService.js';

/**
 * 用法:
 * - 全量：cd server && pnpm exec tsx src/scripts/run-baijiahao-work-daily-import.ts
 * - 单账号：cd server && pnpm exec tsx src/scripts/run-baijiahao-work-daily-import.ts <accountId>
 */
async function main() {
  try {
    await initDatabase();
    const accountIdArg = process.argv[2];

    logger.info('[BJ WorkDaily] Manual run start...');
    if (accountIdArg) {
      const accountId = parseInt(accountIdArg, 10);
      if (isNaN(accountId)) {
        logger.error('[BJ WorkDaily] accountId 必须是数字');
        process.exit(1);
      }
      await BaijiahaoWorkDailyStatisticsImportService.runDailyImportForAccount(accountId);
    } else {
      await BaijiahaoWorkDailyStatisticsImportService.runDailyImport();
    }
    logger.info('[BJ WorkDaily] Manual run done.');
    process.exit(0);
  } catch (e) {
    logger.error('[BJ WorkDaily] Manual run failed:', e);
    process.exit(1);
  }
}

void main();

