import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, requireSection, AuthedRequest } from '@server/middleware/withAuth';
import { verifyOpenRouterKey } from '@server/services/openrouter-sync.service';

export const POST = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    await requireSection(req, 'ai_config');
    const { restaurantId } = req.session;
    const result = await verifyOpenRouterKey(restaurantId ?? null);
    return NextResponse.json(result, { status: result.ok ? 200 : 400 });
  })
);
