import { cleanupExpiredUploads } from './tus';

// 设置定时清理任务 - 每天凌晨执行
export function startCleanupScheduler() {
	// 每 24 小时执行一次清理任务
	const CLEANUP_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours in milliseconds

	// 立即执行一次清理
	setTimeout(async () => {
		console.log('Starting initial cleanup...');
		try {
			await cleanupExpiredUploads();
		} catch (error) {
			console.error('Initial cleanup failed:', error);
		}
	}, 5000); // 启动 5 秒后执行

	// 设置定期清理
	setInterval(async () => {
		console.log('Starting scheduled cleanup...');
		try {
			await cleanupExpiredUploads();
		} catch (error) {
			console.error('Scheduled cleanup failed:', error);
		}
	}, CLEANUP_INTERVAL);

	console.log('Upload cleanup scheduler started - will run every 24 hours');
}

// 手动触发清理（可用于 API 调用）
export async function triggerCleanup() {
	console.log('Manual cleanup triggered...');
	try {
		return await cleanupExpiredUploads();
	} catch (error) {
		console.error('Manual cleanup failed:', error);
		throw error;
	}
}
