import type { APIRoute } from 'astro';
import { syncFeed } from '../../../../../lib/feeds/sync-engine.js';

/** POST /api/admin/feeds/[id]/sync — Force sync */
export const POST: APIRoute = async ({ params }) => {
  try {
    const id = parseInt(params.id!);
    if (isNaN(id)) {
      return new Response(JSON.stringify({ error: 'Invalid feed ID' }), { status: 400 });
    }

    const stats = await syncFeed(id);

    return new Response(JSON.stringify({ success: true, stats }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' },
    });
  } catch (err: any) {
    return new Response(JSON.stringify({ error: err.message }), { status: 500 });
  }
};
