import { BadRequestException, Body, Controller, Headers, HttpCode, Post } from '@nestjs/common';
import type { IngestInteractionRequest, IngestInteractionResponse } from '@insignia/iios-contracts';
import { IngestService } from './ingest.service';
import { IngestRequestDto } from './ingest.dto';

@Controller('v1/interactions')
export class InteractionsController {
  constructor(private readonly ingestService: IngestService) {}

  @Post('ingest')
  @HttpCode(202)
  async ingest(
    @Body() body: IngestRequestDto,
    @Headers('idempotency-key') idempotencyKey?: string,
  ): Promise<IngestInteractionResponse> {
    if (!idempotencyKey) throw new BadRequestException('Idempotency-Key header is required');
    return this.ingestService.ingest(body as unknown as IngestInteractionRequest, idempotencyKey);
  }
}
