import { NextResponse } from 'next/server';

import { type RouteHandler } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { errorResponse } from '@/lib/errors';
import { transferOwnership } from '@/lib/services/yachts.service';
import { transferOwnershipSchema } from '@/lib/validators/yachts';

export const transferHandler: RouteHandler = async (req, ctx, params) => {
  try {
    const body = await parseBody(req, transferOwnershipSchema);
    const yacht = await transferOwnership(params.id!, ctx.portId, body, {
      userId: ctx.userId,
      portId: ctx.portId,
      ipAddress: ctx.ipAddress,
      userAgent: ctx.userAgent,
    });
    return NextResponse.json({ data: yacht });
  } catch (error) {
    return errorResponse(error);
  }
};
