import { Module } from '@nestjs/common';
import { AiProviderService } from './core/ai-provider.service';
import { InvocationRecorderService } from './core/invocation-recorder.service';
import { PromptCacheService } from './core/prompt-cache.service';
import { SafetyGateService } from './core/safety-gate.service';
import { AiCallRunnerService } from './ai-call-runner.service';
import { DraftPlanScriptCall } from './calls/draft-plan-script/tiers/stable/stable.call';
import { StandardScriptCall } from './calls/draft-plan-script/tiers/standard/standard.call';
import { DeepPlanCall, DeepWriteCall, DeepVerifyCall } from './calls/draft-plan-script/tiers/deep/calls';
import { DeepScriptStrategy } from './calls/draft-plan-script/tiers/deep/deep.strategy';
import { WecomWriteCall } from './calls/draft-wecom-script/calls';
import { WecomScriptStrategy } from './calls/draft-wecom-script/wecom.strategy';
import { WecomScriptOrchestrator } from './orchestrators/wecom-script.orchestrator';
import { DraftPlanScriptSkillRegistry } from './calls/draft-plan-script/shared/skill-registry.service';
import { DraftPlanSummaryCall } from './calls/draft-plan-summary/call';
import { DraftRecallSummaryCall } from './calls/draft-recall-summary/call';
import { DraftPersonaSummaryCall } from './calls/draft-persona-summary/call';
import { DraftRecallBriefCall } from './calls/draft-recall-brief/call';
import { PlanScriptOrchestrator } from './orchestrators/plan-script.orchestrator';
import { PlanSummaryOrchestrator } from './orchestrators/plan-summary.orchestrator';
import { RecallSummaryOrchestrator } from './orchestrators/recall-summary.orchestrator';
import { PersonaSummaryOrchestrator } from './orchestrators/persona-summary.orchestrator';
import { RecallBriefOrchestrator } from './orchestrators/recall-brief.orchestrator';
import { PlanModule } from '../plan/plan.module';

/**
 * AiModule — harness 入口。
 *
 * 分层:
 *   core/             基建(provider 工厂、缓存、安全门、调用记录)
 *   calls/            一个目录一个 AiCall(prompt + schema + safety rules + fallback)
 *   orchestrators/    业务编排(从 DB 装 input → runner.run → 落业务表)
 *   ai-call-runner    harness 统一执行器(任何 AiCall 都走这里)
 *
 * 加新 AiCall 步骤:
 *   1. calls/<call-key>/ 下放 prompt.ts + schema.ts + input.types.ts + call.ts
 *   2. 在本 module providers + exports 注册
 *   3. 在 orchestrators/ 下加一个业务编排(读 DB、装 input、调 runner、写业务表)
 *
 * 加新 orchestrator(如 plan-summary)同样套这个模板。
 */
@Module({
  imports: [PlanModule], // 给 PlanSummaryOrchestrator 用 ChainComposerService
  providers: [
    // core
    AiProviderService,
    InvocationRecorderService,
    PromptCacheService,
    SafetyGateService,
    // harness
    AiCallRunnerService,
    // AI calls — 话术三档(投入档:stable / standard / deep),共用 runner/input/safety/fallback
    DraftPlanScriptCall, // 稳健档
    StandardScriptCall, // 标准档
    DeepPlanCall, // 深度档 步骤1 规划
    DeepWriteCall, // 深度档 步骤2 写(多段)
    DeepVerifyCall, // 深度档 步骤3 独立对抗校验
    DeepScriptStrategy, // 深度档 3 步编排(plan→write→verify→repair→兜底)
    // AI calls — 企微话术(只有深度档;一次性单块,⛔ 无模板兜底,见 wecom.strategy 文件头)
    WecomWriteCall,
    WecomScriptStrategy,
    DraftPlanScriptSkillRegistry, // scan & cache draft-plan-script/**​/skills/**​/SKILL.md
    DraftPlanSummaryCall,
    DraftRecallSummaryCall,
    DraftPersonaSummaryCall,
    DraftRecallBriefCall,
    // orchestrators
    PlanScriptOrchestrator,
    WecomScriptOrchestrator,
    PlanSummaryOrchestrator,
    RecallSummaryOrchestrator,
    PersonaSummaryOrchestrator,
    RecallBriefOrchestrator,
  ],
  exports: [
    // 对外暴露 orchestrator(业务方调用入口)+ runner(高级使用 / eval CLI)
    // 助手每轮往 agent_invocations 落一行,复用同一个 recorder(⛔ 别再抄一份写库逻辑)
    InvocationRecorderService,
    PlanScriptOrchestrator,
    WecomScriptOrchestrator, // 企微话术(PlansAggregateController 注入)
    PlanSummaryOrchestrator,
    RecallSummaryOrchestrator,
    PersonaSummaryOrchestrator,
    RecallBriefOrchestrator,
    AiCallRunnerService,
    AiProviderService,
    // 实时坐席辅助教练复用:skills 注册表(组装 Qwen instructions)
    DraftPlanScriptSkillRegistry,
  ],
})
export class AiModule {}
