import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { withValidationAuthed } from '@server/middleware/withValidation';
import { createSipLineSchema } from '@server/validators/telephone.validator';
import { listSipLines, createSipLine } from '@server/services/telephone/sip.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const lines = await listSipLines(req.session.restaurantId!);
    return NextResponse.json({ lines });
  })
);

export const POST = withErrorHandler(
  withAuth(
    withValidationAuthed(createSipLineSchema, async (req) => {
      const line = await createSipLine(req.session.restaurantId!, req.parsedBody as Record<string, unknown> as Parameters<typeof createSipLine>[1]);
      return NextResponse.json({ line }, { status: 201 });
    })
  )
);
