import { createZodDto } from 'nestjs-zod';
import { z } from 'zod';
import {
  ListHostsResponseSchema,
  HostSummarySchema,
  HostDetailSchema,
  HostStatsSchema,
  RotatePushSecretResponseSchema,
  RotateCallbackSecretResponseSchema,
  RotateSecretResponseSchema,
  RemovePushSecretRequestSchema,
  RemovePushSecretResponseSchema,
  UpdateHostRequestSchema,
  AiInvocationListQuerySchema,
  AiInvocationDetailSchema,
  AiInvocationStatsSchema,
  ListAiInvocationsResponseSchema,
} from '@pac/types';

export class ListHostsResponseDto extends createZodDto(ListHostsResponseSchema) {}
export class HostSummaryDto extends createZodDto(HostSummarySchema) {}
export class HostDetailDto extends createZodDto(HostDetailSchema) {}
export class HostStatsDto extends createZodDto(HostStatsSchema) {}
export class UpdateHostRequestDto extends createZodDto(UpdateHostRequestSchema) {}
export class RotateSecretResponseDto extends createZodDto(RotateSecretResponseSchema) {}
export class RotatePushSecretResponseDto extends createZodDto(RotatePushSecretResponseSchema) {}
export class RotateCallbackSecretResponseDto extends createZodDto(RotateCallbackSecretResponseSchema) {}
export class RemovePushSecretRequestDto extends createZodDto(RemovePushSecretRequestSchema) {}
export class RemovePushSecretResponseDto extends createZodDto(RemovePushSecretResponseSchema) {}

// AI Invocations
export class AiInvocationListQueryDto extends createZodDto(AiInvocationListQuerySchema) {}
export class AiInvocationDetailDto extends createZodDto(AiInvocationDetailSchema) {}
export class AiInvocationStatsDto extends createZodDto(AiInvocationStatsSchema) {}
export class ListAiInvocationsResponseDto extends createZodDto(ListAiInvocationsResponseSchema) {}

// Cold-import 网页上传(host-self)
const ColdImportUploadResponseSchema = z.object({ jobId: z.string() });
export class ColdImportUploadResponseDto extends createZodDto(ColdImportUploadResponseSchema) {}

// 进度:BullMQ job 状态 + 完成时的 ImportRunResult(含 totals);job 已清理/不存在 → state='not_found'
const ColdImportStatusSchema = z.object({
  state: z.string(),
  result: z.any().nullable(),
  failedReason: z.string().nullable(),
  // 运行中的真实进度(cohortDone/cohortTotal 批次);非运行态或 dryRun 为 null
  progress: z.object({ done: z.number(), total: z.number() }).nullable(),
});
export class ColdImportStatusDto extends createZodDto(ColdImportStatusSchema) {}

/// 宿主推送记录(sync_logs direction=push 的对宿主视图)—— 对接期自助排查:
/// 哪一批、什么时候、收了几行、落了几条、去重几条、失败几条、错在哪。
const PushLogSchema = z.object({
  id: z.string(),
  source: z.string().describe('推送的源表名'),
  status: z.string().describe('success / partial / failed'),
  dryRun: z.boolean().describe('true = 预检批次(未落库)'),
  fetched: z.number().int().describe('收到的行数'),
  transactionsWritten: z.number().int().describe('实际落库事件数(与 fetched 的差 = 被 PAC 规则过滤)'),
  duplicates: z.number().int().describe('幂等命中数(重推安全的证据)'),
  failed: z.number().int(),
  errorMessage: z.string().nullable(),
  startedAt: z.string(),
  endedAt: z.string().nullable(),
});
const PushLogsSchema = z.object({ items: z.array(PushLogSchema) });
export class PushLogsDto extends createZodDto(PushLogsSchema) {}
