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

export const POST = withErrorHandler(
  withAuth(async (req: AuthedRequest, ctx: RouteContext) => {
    await requireSection(req, 'coupons', 'create');
    await requirePlanFeature(req.session.restaurantId!, 'coupons');
    const { id } = await ctx.params;
    const coupon = await duplicateCoupon(id, req.session.restaurantId!);
    return NextResponse.json({ coupon }, { status: 201 });
  })
);
