import { Controller, Get, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Permission } from '@pac/types';
import { RequirePermission } from '../../common/decorators/permissions.decorator';
import {
  TenantScope,
  TenantScopeContext,
} from '../../common/decorators/tenant-scope.decorator';
import { MappingMissService } from './mapping-miss.service';

/**
 * 映射覆盖漏审计 — GET /admin/mapping-miss
 *
 * 看"摄入时哪些原值没映射上、落了 _default"(如诊断名长尾 / 获客渠道长尾),按出现量倒序。
 * 闭环:看到 → 扩 assembler enum/keyword yaml → reparse → 该值不再 miss。
 *
 * Scope:host self(只看自家 host_id)。权限 PLATFORM_MANAGE。
 */
@ApiTags('admin')
@ApiBearerAuth('accessToken')
@Controller('admin/mapping-miss')
@RequirePermission(Permission.PLATFORM_MANAGE)
export class MappingMissAdminController {
  constructor(private readonly svc: MappingMissService) {}

  @Get()
  @ApiOperation({
    summary:
      '摄入漂移报告:misses(落 _default 的原值,扩字典)+ suspectFields(整列缺席,疑似宿主改表)。扫最近 N 次 sync run 的 metadata(默认 100,上限 500)',
  })
  recent(@TenantScope() scope: TenantScopeContext, @Query('limit') limit?: string) {
    return this.svc.recent(scope, { limit: limit ? Number(limit) : undefined });
  }
}
