/**
 * Route handler for `/api/v1/interests/[id]/berths/[berthId]/price` (CM-2 Part B).
 *
 * Sets or clears the deal-specific price override for one (interest, berth).
 * In handlers.ts so integration tests can call it directly.
 */

import { NextResponse } from 'next/server';
import { z } from 'zod';

import { type RouteHandler } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { errorResponse } from '@/lib/errors';
import { setBerthPriceOverride } from '@/lib/services/interest-berths.service';

const bodySchema = z.object({
  price: z.number().nonnegative().nullable(),
  currency: z.string().min(1).max(8).optional(),
});

export const putHandler: RouteHandler<{ id: string; berthId: string }> = async (
  req,
  ctx,
  params,
) => {
  try {
    const body = await parseBody(req, bodySchema);
    await setBerthPriceOverride(
      params.id!,
      params.berthId!,
      body.price,
      body.currency ?? null,
      ctx.portId,
    );
    return new NextResponse(null, { status: 204 });
  } catch (error) {
    return errorResponse(error);
  }
};
