import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { listCallLogs } from '@server/services/call-logs.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const { restaurantId } = req.session;
    const url = new URL(req.url);
    const p = url.searchParams;

    const result = await listCallLogs({
      restaurantId: restaurantId!,
      direction: p.get('direction') ?? undefined,
      status: p.get('status') ?? undefined,
      search: p.get('search') ?? undefined,
      dateFrom: p.get('date_from') ?? undefined,
      dateTo: p.get('date_to') ?? undefined,
      page: parseInt(p.get('page') ?? '1', 10),
      limit: parseInt(p.get('limit') ?? '20', 10),
    });

    return NextResponse.json({
      callLogs: result.data,
      total: result.total,
      page: result.page,
      limit: result.limit,
      pages: result.pages,
    });
  })
);
