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 { createAgentSchema } from '@server/validators/ai-agents.validator';
import { listAgents, createAgent } from '@server/services/ai-agents.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const { restaurantId, branchId } = req.session;
    const agents = await listAgents(restaurantId!, branchId ?? null);
    return NextResponse.json({ agents });
  })
);

export const POST = withErrorHandler(
  withAuth(
    withValidationAuthed(createAgentSchema, async (req) => {
      const { restaurantId, branchId } = req.session;
      const agent = await createAgent(restaurantId!, req.parsedBody as Record<string, unknown>, branchId ?? null);
      return NextResponse.json({ agent }, { status: 201 });
    })
  )
);
