/**
 * POST /api/sync/schedules/reload
 *   Stops every running cron task and re-loads from the DB. Use after seeding
 *   new schedule rows directly via SQL (the scheduler only seeds defaults on a
 *   virgin table).
 */

import { NextResponse } from 'next/server';
import { requirePermission } from '@/lib/auth-utils';
import { syncScheduler } from '@/lib/services/sync-scheduler';

export async function POST() {
  const { error } = await requirePermission('admin', 'access');
  if (error) return error;

  try {
    const result = await syncScheduler.reloadAllSchedules();
    return NextResponse.json({ ok: true, ...result });
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    console.error('[SCHEDULE API] reload failed:', message);
    return NextResponse.json(
      { error: 'Failed to reload schedules', details: message },
      { status: 500 }
    );
  }
}
