import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
import {
  PortalRequestCreateSchema,
  type PortalRequestCreate,
} from '@siop/shared';
import { Public } from '../auth/public.decorator';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { PortalService } from './portal.service';

/** Portail public (QR cabine) : seules routes @Public métier de l'API —
 *  protégées par throttling (surface exposée sans compte). */
@Controller('portal')
@UseGuards(ThrottlerGuard)
export class PortalController {
  constructor(private readonly portal: PortalService) {}

  @Public()
  @Get('assets/:reference')
  @Throttle({ default: { ttl: 60_000, limit: 60 } })
  resolveAsset(@Param('reference') reference: string) {
    return this.portal.resolveAsset(reference);
  }

  @Public()
  @Post('requests')
  @Throttle({ default: { ttl: 60_000, limit: 10 } }) // anti-spam signalement
  createRequest(
    @Body(new ZodValidationPipe(PortalRequestCreateSchema)) body: PortalRequestCreate,
  ) {
    return this.portal.createRequest(body);
  }

  @Public()
  @Get('requests/:reference/:token')
  @Throttle({ default: { ttl: 60_000, limit: 60 } })
  status(@Param('reference') reference: string, @Param('token') token: string) {
    return this.portal.status(reference, token);
  }
}
