import { Body, Controller, Post } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ZodResponse } from 'nestjs-zod';
import { Permission } from '@pac/types';
import { RequirePermission } from '../../common/decorators/permissions.decorator';
import {
  TenantScope,
  TenantScopeContext,
} from '../../common/decorators/tenant-scope.decorator';
import { IngestFactsRequestDto, IngestFactsResponseDto } from './dto/facts.dto';
import { FactsService } from './facts.service';

@ApiTags('facts')
@ApiBearerAuth('accessToken')
@Controller('facts')
export class FactsController {
  constructor(private readonly facts: FactsService) {}

  @Post()
  @ZodResponse({ status: 202, type: IngestFactsResponseDto })
  @RequirePermission(Permission.FACT_INGEST)
  @ApiOperation({
    summary: 'API Pool 1 — push patient fact events into the timeline',
    description:
      'Accepts a batch of fact events. Idempotency on (hostId, tenantId, externalRef) ' +
      'is the caller responsibility for now. Triggers persona / plan recompute downstream.',
  })
  ingest(@TenantScope() scope: TenantScopeContext, @Body() body: IngestFactsRequestDto) {
    return this.facts.ingest(scope, body);
  }
}
