import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, requireSection, AuthedRequest } from '@server/middleware/withAuth';
import { bulkImportSchema } from '@server/validators/coupons.validator';
import { bulkImportCoupons } from '@server/services/coupons.service';
import { requirePlanFeature } from '@server/utils/features';

export const POST = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    await requireSection(req, 'coupons', 'create');
    await requirePlanFeature(req.session.restaurantId!, 'coupons');
    const body = await req.json();
    const parsed = bulkImportSchema.parse(body);
    const result = await bulkImportCoupons(req.session.restaurantId!, parsed);
    return NextResponse.json(result);
  })
);
