/**
 * 清空 Redis 开发库（本地干净环境，快速失败）
 * 用法：npx tsx scripts/dev/flush-redis.ts
 */
import Redis from "ioredis";
import { loadDevEnv } from "../../lib/dev-env";

loadDevEnv();

const FLUSH_TIMEOUT_MS = Number(process.env.REDIS_FLUSH_TIMEOUT_MS ?? 3000);

async function main(): Promise<void> {
  const url = process.env.REDIS_URL;
  if (!url) {
    throw new Error("REDIS_URL 未配置");
  }

  const redis = new Redis(url, {
    family: 4,
    lazyConnect: true,
    connectTimeout: FLUSH_TIMEOUT_MS,
    maxRetriesPerRequest: 1,
    enableReadyCheck: true,
    retryStrategy: () => null,
  });

  try {
    await redis.connect();
    await redis.flushdb();
    console.log("Redis FLUSHDB 完成");
  } finally {
    await redis.quit().catch(() => undefined);
  }
}

main().catch((error: unknown) => {
  console.error(error instanceof Error ? error.message : error);
  process.exit(1);
});
