import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import {
  AssistantAskSchema,
  SuggestBilanSchema,
  type AssistantAsk,
  type SuggestBilan,
} from '@siop/shared';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { AssistantService } from './assistant.service';

@Controller('assistant')
export class AssistantController {
  constructor(private readonly assistant: AssistantService) {}

  /** Poser une question — qui lit les OT peut interroger le corpus. */
  @Post('ask')
  @HttpCode(200)
  @RequirePermission('WORK_ORDERS', 'view')
  ask(@Body(new ZodValidationPipe(AssistantAskSchema)) body: AssistantAsk) {
    return this.assistant.ask(body);
  }

  /** Réindexer le corpus — même droit que la gestion du référentiel (D3). */
  @Post('reindex')
  @HttpCode(200)
  @RequirePermission('ASSETS', 'edit')
  reindex() {
    return this.assistant.reindex();
  }

  /** Suggérer des codes — réservé à qui remplit des bilans (D1). */
  @Post('suggest-bilan')
  @HttpCode(200)
  @RequirePermission('WORK_ORDERS', 'edit')
  suggest(@Body(new ZodValidationPipe(SuggestBilanSchema)) body: SuggestBilan) {
    return this.assistant.suggestBilan(body);
  }
}
