import { z } from 'zod';
import { AgentService } from '../services/ai/AgentService';
import type { AsyncRequestHandler } from '../types/http';
import { AppError } from '../utils/AppError';

const agentRequestSchema = z.object({
  message: z.string().min(1),
  context: z.record(z.string(), z.unknown()).optional(),
  maxSteps: z.number().int().positive().max(10).optional(),
});

export class AgentController {
  constructor(private readonly service = new AgentService()) {}

  chat: AsyncRequestHandler = async (req, res) => {
    const input = agentRequestSchema.parse(req.body);

    if (!req.authContext) {
      throw new AppError('Authenticated finance user context was not found.', 401, 'AUTH_CONTEXT_MISSING');
    }

    const response = await this.service.handle(input, req.authContext);
    res.json(response);
  };
}
