import { Injectable, Logger, NotImplementedException } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { PrismaService } from '../../prisma/prisma.service';
import { FactsService } from '../facts/facts.service';
import { AlertService } from '../../common/alerting/alert.service';
import { PullStrategyRegistry } from './adapters/adapter.registry';

/**
 * SyncService — 增量拉取 + 漂移检测 orchestration
 *
 * 暂未实施。当前数据接入只走 cold-import(`pnpm cold-import`,见 ColdImportService);
 * pull / push 路径待 host 真实 API 就绪后再启用,届时复用 AssemblerEngine + TransactionSynthesizer
 * + ParserPipeline 三件套(已实现)。
 */
@Injectable()
export class SyncService {
  private readonly logger = new Logger(SyncService.name);

  constructor(
    private readonly prisma: PrismaService,
    private readonly facts: FactsService,
    private readonly registry: PullStrategyRegistry,
    private readonly alerter: AlertService,
  ) {}

  /**
   * Sync 拉数据 cron — 每小时跑一次(W2 决策:对召回业务小时级足够 + 不压宿主)。
   * 事件驱动主路径(秒级响应)由 pipeline-dispatcher 入队 persona-recompute,本 cron 只是兜底拉数据。
   */
  @Cron(CronExpression.EVERY_HOUR, { name: 'sync-scheduled-tick' })
  async runScheduledForAll(): Promise<void> {
    // 暂未启用 — pull 通道接通后挂回本 cron
  }

  async runPull(_hostId: string): Promise<{ runId: string; emitted: number }> {
    throw new NotImplementedException(
      'Pull 通道暂未实施,数据请通过 cold-import 入站(见 pnpm cold-import)',
    );
  }

  async runPullForPatient(
    _scope: unknown,
    _patientId: string,
  ): Promise<{ syncLogId: string; status: string; dedupedFromExisting?: boolean }> {
    throw new NotImplementedException(
      '单患者 pull 暂未实施,数据请通过 cold-import 入站',
    );
  }

  async checkPullDrift(): Promise<void> {
    // 暂未启用 — pull 通道接通后启用漂移监控
  }
}
