import { Injectable, NotFoundException } from '@nestjs/common';
import type { Prisma } from '@prisma/client';
import { maskName, maskPhone } from '@pac/utils';
import { applyLiveDays } from '@pac/types';
import { PrismaService } from '../../prisma/prisma.service';
import { ChainComposerService } from '../plan/engine/chain-composer.service';
import type { TenantScopeContext } from '../../common/decorators/tenant-scope.decorator';
import { toothSet } from '../sync/pipeline/parsers/tooth-position.util';

/**
 * PlanAggregateService — Plan 详情聚合服务。
 *
 * 一次拉全:patient + profile + plan + reasons + persona + features + facts + chains。
 *
 * 注:命名是历史(早期只给 demo 页用),现在也是 (app)/plans/[planId] 的数据源。
 * 治疗链合成委托给 ChainComposerService(共享逻辑,PlanEngine / PersonaService 同源)。
 */
@Injectable()
export class PlanAggregateService {
  constructor(
    private readonly prisma: PrismaService,
    private readonly chainComposer: ChainComposerService,
  ) {}

  /**
   * 按 planId 查 → 反查 patient → 同 assemble。
   * (app)/plans/[planId] 路由 + PlansAggregateController 的入口。
   */
  async getPlanDetailByPlanId(scope: TenantScopeContext, planId: string) {
    const plan = await this.prisma.followupPlan.findUnique({
      where: { id: planId },
      include: { reasons: { orderBy: { priorityScore: 'desc' } } },
    });
    if (!plan) throw new NotFoundException(`Plan ${planId} not found`);
    if (plan.hostId !== scope.hostId || plan.tenantId !== scope.tenantId) {
      throw new NotFoundException(`Plan ${planId} not found`);
    }
    const patient = await this.prisma.patient.findUnique({
      where: { id: plan.patientId },
      include: {
        profile: true,
        // 联系人/亲属边 → 关系人姓名/电话从 relatedPatient 现取(patient↔patient,零冗余)
        relationsOut: {
          include: { relatedPatient: { select: { name: true, phone: true } } },
        },
        // 诊所回访记录(展示用,按时间倒序,封顶 100;每患者 p99=19,够用)
        returnVisits: { orderBy: { taskDate: 'desc' }, take: 100 },
      },
    });
    if (!patient) throw new NotFoundException(`Patient ${plan.patientId} not found`);

    // supersede 跳转:plan 重算后会 supersede 出新版本(新 id),老 plan 行仍保留冻结快照。
    // 老 plan URL(收藏 / 分享 / 任务抽屉 / 刷新后 refetch)若直接渲染会显示陈旧数据。
    // → 检测到请求的是 superseded plan,解析该患者当前 active/assigned plan,响应带 currentPlanId,
    //   前端 router.replace 落到最新版(单 patient 仅一个活跃 plan,partial UNIQUE 保证)。
    let currentPlanId = plan.id;
    if (plan.status === 'superseded') {
      const active = await this.prisma.followupPlan.findFirst({
        where: {
          patientId: plan.patientId,
          hostId: scope.hostId,
          tenantId: scope.tenantId,
          status: { in: ['active', 'assigned'] },
        },
        orderBy: { version: 'desc' },
        select: { id: true },
      });
      if (active) currentPlanId = active.id;
    }

    const assembled = await this.assemble(scope, patient, plan);
    return { ...assembled, currentPlanId };
  }

  // ─────────────────────────────────────────────
  // 共享 assemble(patient + plan 已加载,组装其他)
  // ─────────────────────────────────────────────

  private async assemble(
    scope: TenantScopeContext,
    patient: Prisma.PatientGetPayload<{
      include: {
        profile: true;
        relationsOut: { include: { relatedPatient: { select: { name: true; phone: true } } } };
        returnVisits: true;
      };
    }>,
    plan: Prisma.FollowupPlanGetPayload<{ include: { reasons: true } }> | null,
  ) {
    const persona = await this.loadCurrentPersona(patient.id);
    const facts = await this.loadActiveFacts(scope, patient.id);
    // W4:话术从 DB 加载(LLM 流式生成完会 upsert 到 plan_scripts)
    // 没生成过 → script=null,前端走 mock 兜底
    const script = plan ? await this.loadPlanScript(plan.id) : null;
    // v2.1:chain-composer 读独立 diagnosis_record / treatment_record / recommendation_record,
    //       传所有 facts,内部按 type 分组(encounter_record 已只元数据)
    const chains = this.chainComposer.compose(facts);
    // ⭐ SQL 为准:chain ★ 按 plan_reasons (code, tooth) 联合对齐(W4 末)
    //
    // 规则:reason 是 (code + tooth) tuple → 找 chain.code 相同 + tooth overlap
    //   林兆星案例:
    //     K08 reason tooth=17(SQL 召回的是这颗未做)
    //     chain 有 4 条 K08:11;12;21;22 / 15;16;24 / 17 / 未标注
    //     正确 target:只有 chain 17 应 ★(其他都已做 actual implant,SQL ⑤a 排除)
    //   K05 reason tooth=null(全口诊断)→ 任何 K05 chain 都对齐(只 1 条牙周·全口)
    //
    // 1 年前 fulfilled 预约让 chain 算 entered 也照样 ★(SQL 是真理 — 没坚持治疗就该召回)
    const reasonKeys = new Set<string>(); // key = "code|tooth" or "code|*"(全口)
    for (const r of plan?.reasons ?? []) {
      const sigs = (r.signals ?? {}) as { triggers?: Array<{ code?: string }>; toothPosition?: string | null };
      const tooth = (sigs.toothPosition ?? '').trim();
      for (const t of sigs.triggers ?? []) {
        if (!t.code) continue;
        reasonKeys.add(`${t.code}|${tooth || '*'}`);
      }
    }
    // W4 末:牙位精确匹配(Palmer 乳牙 + FDI 恒牙都支持)
    //   旧版 strip 所有字母 → "1D OD" → "1" → Palmer 乳牙 1A/1B/1C/1D/1E 全部误匹配
    //   新版用 toothSet:剥面后缀后保留牙位 base("1D OD" → "1D"; "11 M" → "11")
    //   精确匹配避免 Palmer 5 颗乳牙在儿童 case 被误判
    const toothOverlap = (a: string, b: string): boolean => {
      if (!a || !b) return false;
      const A = toothSet(a);
      if (A.size === 0) return false;
      for (const t of toothSet(b)) {
        if (A.has(t)) return true;
      }
      return false;
    };
    for (const c of chains) {
      if (!c.code) { c.target = false; continue; }
      // closed chain 永不 ★(临床已结束;即使 SQL 召回也是老 plan/data race 异常)
      // K04 46 拔了/被替代关闭 → 即使老 plan reason 还含 K04 也不该 focused
      if (c.status === 'closed') { c.target = false; continue; }
      let hit = false;
      for (const key of reasonKeys) {
        const [code, tooth] = key.split('|');
        if (code !== c.code) continue;
        // reason 无牙位(全口诊断 K05)→ 任何 chain.code 同就命中
        if (tooth === '*') { hit = true; break; }
        // reason 有牙位 → 跟 chain.toothPosition overlap
        if (c.toothPosition && toothOverlap(tooth!, c.toothPosition)) { hit = true; break; }
      }
      c.target = hit;
    }

    // 召回历史(患者级,跨所有 plan 版本)—— 再次召回是新 plan id,旧执行挂在旧 plan 上,
    // 必须按 patient 取才看得到"上次为什么被压住 / 上次结果"。也顺带补上之前缺失的通话历史。
    const recallHistory = await this.loadRecallHistory(scope, patient.id);

    return {
      patient: serializePatient(patient),
      profile: serializeProfile(patient, facts, facts.filter((f) => f.type === 'encounter_record')),
      plan: plan ? serializePlan(plan) : null,
      persona: persona ? serializePersona(persona) : null,
      chains,
      facts: facts.map(serializeFact),
      script: script ? serializeScript(script) : null,
      recallHistory,
      returnVisits: (patient.returnVisits ?? []).map((r) => ({
        taskDate: r.taskDate ? r.taskDate.toISOString().slice(0, 10) : null,
        type: r.type,
        status: r.status,
        taskStatus: r.taskStatus,
        treatmentItems: r.treatmentItems,
        followContent: r.followContent,
        result: r.result,
      })),
    };
  }

  /**
   * 患者级召回历史 —— 最近 N 次执行结果(跨该患者所有 plan 版本)。
   * 用途:plan 再次被召回时,客服能看到"上次召回结果 / 为什么被暂缓"(终态 outcome 即原因)。
   */
  private async loadRecallHistory(scope: TenantScopeContext, patientId: string) {
    const rows = await this.prisma.planExecution.findMany({
      where: { hostId: scope.hostId, tenantId: scope.tenantId, plan: { patientId } },
      orderBy: { createdAt: 'desc' },
      take: 8,
      select: {
        id: true,
        outcome: true,
        channel: true,
        notes: true,
        scheduledNextAt: true,
        createdAt: true,
        plan: { select: { version: true } },
      },
    });
    return rows.map((e) => ({
      id: e.id,
      outcome: e.outcome,
      channel: e.channel,
      notes: e.notes,
      scheduledNextAt: e.scheduledNextAt?.toISOString() ?? null,
      createdAt: e.createdAt.toISOString(),
      planVersion: e.plan?.version ?? null,
    }));
  }

  /**
   * W4:加载该 plan 的最新 ready 话术(LLM 生成完会 upsert 进 plan_scripts)。
   * pending/failed 的不返回 — 前端走 mock 兜底,客服点"重新生成"再触发 LLM。
   */
  private loadPlanScript(planId: string) {
    return this.prisma.planScript.findUnique({
      where: { planId },
    });
  }

  // ─────────────────────────────────────────────
  // 数据加载(分小函数 — 便于测试 + 单一职责)
  // ─────────────────────────────────────────────

  private loadCurrentPersona(patientId: string) {
    return this.prisma.persona.findFirst({
      where: { patientId, supersededAt: null },
      include: { features: { orderBy: { createdAt: 'asc' } } },
      orderBy: { version: 'desc' },
    });
  }

  private loadActiveFacts(scope: TenantScopeContext, patientId: string) {
    return this.prisma.patientFact.findMany({
      where: {
        hostId: scope.hostId,
        tenantId: scope.tenantId,
        patientId,
        // ⚠️ 不能只 active — treatment_record(actual) 完成后 status='fulfilled',
        // appointment_record 到诊后也 fulfilled。这些都属"现行已发生事实"应展示。
        // 排除的是被 superseded / cancelled / expired / invalidated 的历史/失效版本。
        status: { in: ['active', 'fulfilled'] },
      },
      orderBy: { occurredAt: 'desc' },
    });
  }
}

// ─────────────────────────────────────────────
// Serializers(纯函数 — 易测、无副作用)
// ─────────────────────────────────────────────

type PatientWithProfile = NonNullable<
  Awaited<ReturnType<PlanAggregateService['getPlanDetailByPlanId']>>['plan']
> extends never
  ? never
  : Parameters<typeof serializePatient>[0];

type Fact = Awaited<ReturnType<typeof loadFactStub>>[number];
// 类型 helper — 仅供推导用,不真调用
declare function loadFactStub(): Promise<
  Awaited<ReturnType<PlanAggregateService['getPlanDetailByPlanId']>>['facts']
>;

/// 从 patients.preferences JSON 安全解析专属客服(摄入时 mergePatientPreferences 写入)
function extractDedicatedCs(prefs: unknown): { id: string; name: string } | null {
  if (!prefs || typeof prefs !== 'object') return null;
  const d = (prefs as Record<string, unknown>).dedicatedCs;
  if (!d || typeof d !== 'object') return null;
  const name = String((d as Record<string, unknown>).name ?? '').trim();
  const id = String((d as Record<string, unknown>).id ?? '').trim();
  return name ? { id, name } : null;
}

function serializePatient(patient: {
  id: string;
  externalId: string;
  medicalRecordNumber: string | null;
  name: string | null;
  gender: string | null;
  birthDate: Date | null;
  phone: string | null;
  active: boolean;
  preferences: unknown;
  profile: { tags: string[] } | null;
}) {
  return {
    id: patient.id,
    externalId: patient.externalId,
    /// 病历号(纸质档案 / 客服核对身份用的可读编号,如 "SH0Q011691");≠ externalId(host patient_id)
    medicalRecordNumber: patient.medicalRecordNumber,
    name: patient.name,
    nameMasked: maskName(patient.name),
    gender: patient.gender,
    birthDate: patient.birthDate?.toISOString().slice(0, 10) ?? null,
    age: patient.birthDate ? calcAge(patient.birthDate) : null,
    phone: patient.phone,
    phoneMasked: maskPhone(patient.phone),
    active: patient.active,
    /// 专属客服({id,name})— 从 preferences.dedicatedCs 解析;无则 null
    dedicatedCs: extractDedicatedCs(patient.preferences),
    tags: patient.profile?.tags ?? [],
  };
}

function serializeProfile(
  patient: {
    profile: {
      doNotContact: boolean;
      deceased: boolean;
      notes: string | null;
    } | null;
    relationsOut?: Array<{
      relationship: string;
      relatedExternalId: string;
      relatedPatientId: string | null;
      relatedPatient: { name: string | null; phone: string | null } | null;
    }>;
  },
  allFacts: Array<{ type: string; content: Prisma.JsonValue; occurredAt: Date | null }>,
  encounters: Array<{ occurredAt: Date | null; content: Prisma.JsonValue }>,
) {
  if (!patient.profile) return null;

  const payments = allFacts.filter((f) => f.type === 'payment_record');
  const recharges = allFacts.filter((f) => f.type === 'recharge_record');
  const refunds = allFacts.filter((f) => f.type === 'refund_record');
  const ltvCents = sumAmount(payments) + sumAmount(recharges) - sumAmount(refunds);

  // 就诊时间口径:
  //   优先用 encounter_record(appointment 显式标"已到诊"的)
  //   兜底 emr_record(医生写病历必然患者到场,assembler 没归 encounter 时不丢)
  //   场景:DW 部分 host 的 appointment.in_time 字段缺失,但 EMR 完整
  //        例 张吴双 — 0 encounter / 39 emr,encounter 兜底空,就诊时间应来自 EMR
  const allVisitFacts = [...encounters, ...allFacts.filter((f) => f.type === 'emr_record')]
    .filter((f) => f.occurredAt)
    .sort((a, b) => (b.occurredAt!.getTime() - a.occurredAt!.getTime()));
  const latestEncounter = allVisitFacts[0];
  const firstEncounter = allVisitFacts.at(-1);
  const daysSinceLatestVisit = latestEncounter?.occurredAt
    ? Math.floor((Date.now() - latestEncounter.occurredAt.getTime()) / 86400_000)
    : null;

  const doctors = Array.from(
    new Set(
      encounters
        .map((e) => (e.content as Record<string, unknown> | null)?.doctor_id as string | undefined)
        .filter(Boolean) as string[],
    ),
  ).slice(0, 2);

  // 联系人/亲属:本人视角(relatedPatient 是本人的 X)。姓名/电话从 relatedPatient 现取。
  //   linked=false(关系人未建档)→ 无姓名,前端展示时跳过。已建档的排前。
  const contacts = (patient.relationsOut ?? [])
    .map((r) => ({
      relationship: r.relationship,
      relationshipLabel: RELATIONSHIP_LABEL[r.relationship] ?? r.relationship,
      name: r.relatedPatient?.name ?? null,
      phone: r.relatedPatient?.phone ?? null,
      linked: !!r.relatedPatientId,
    }))
    .sort((a, b) => Number(b.linked) - Number(a.linked));

  return {
    doNotContact: patient.profile.doNotContact,
    deceased: patient.profile.deceased,
    notes: patient.profile.notes,
    ltvCents,
    paymentCount: payments.length,
    rechargeCount: recharges.length,
    refundCount: refunds.length,
    firstVisit: firstEncounter?.occurredAt?.toISOString().slice(0, 10) ?? null,
    lastVisit: latestEncounter?.occurredAt?.toISOString().slice(0, 10) ?? null,
    daysSinceLatestVisit,
    doctors,
    contacts,
  };
}

/// 关系枚举 → 中文展示标签(本人视角:对方是本人的 X)
const RELATIONSHIP_LABEL: Record<string, string> = {
  mother: '妈妈',
  father: '爸爸',
  grandparent: '祖辈',
  spouse: '配偶',
  child: '子女',
  sibling: '兄弟姐妹',
  grandchild: '孙辈',
  friend: '朋友',
  other: '亲属',
};

function serializePlan(plan: {
  id: string;
  version: number;
  status: string;
  priorityScore: number;
  contactAttempts: number;
  targetClinicId: string | null;
  goal: string | null;
  recommendedAt: Date | null;
  recommendedRole: string | null;
  recommendedChannel: string | null;
  assigneeUserId: string | null;
  assignedAt: Date | null;
  recycleAt: Date | null;
  snoozedUntil: Date | null;
  recallFeedback: string | null;
  recallFeedbackNote: string | null;
  updatedAt: Date;
  reasons: Array<{
    id: string;
    scenario: string;
    subKey: string | null;
    priorityScore: number;
    reason: string;
    lifecycle: string;
    source: string;
    evidence: Prisma.JsonValue;
    breakdown: Prisma.JsonValue;
    signals: Prisma.JsonValue;
  }>;
}) {
  return {
    id: plan.id,
    version: plan.version,
    status: plan.status,
    priorityScore: plan.priorityScore,
    contactAttempts: plan.contactAttempts,
    maxContactAttempts: 4, // 应用层默认(后续可挂 host 配置)
    targetClinicId: plan.targetClinicId,
    goal: plan.goal,
    /// 该 plan 版本的重算时间 — UI "更新于 X" 渲染数据新鲜度
    updatedAt: plan.updatedAt.toISOString(),
    recommendedAt: plan.recommendedAt?.toISOString() ?? null,
    recommendedRole: plan.recommendedRole,
    recommendedChannel: plan.recommendedChannel,
    assigneeUserId: plan.assigneeUserId,
    assignedAt: plan.assignedAt?.toISOString() ?? null,
    recycleAt: plan.recycleAt?.toISOString() ?? null,
    /// 召回冷静期 / 终态抑制窗到期时间(null=无抑制)— 详情页可渲染"已抑制至 / 下次回访 X"
    snoozedUntil: plan.snoozedUntil?.toISOString() ?? null,
    /// 召回反馈(plan 级)— 详情页标题栏拇指当前态;'up' | 'down' | null
    recallFeedback: plan.recallFeedback ?? null,
    recallFeedbackNote: plan.recallFeedbackNote ?? null,
    reasons: plan.reasons.map((r) => ({
      id: r.id,
      scenario: r.scenario,
      subKey: r.subKey,
      priorityScore: r.priorityScore,
      reason: r.reason,
      lifecycle: r.lifecycle,
      source: r.source,
      /// W3 末:evidence 改 JSON 统一(跟 PersonaFeature.evidence 对齐),透 JSON 整体
      evidence: r.evidence,
      /// v2.1:6 因子算法 breakdown + subKey,UI 详情页"为什么 73 分"渲染数据来源
      /// (不含 ruleIds — 算法版本追溯靠 git + plan.createdAt)
      breakdown: r.breakdown,
      /// W3 末:结构化召回信号(raw enum / canonical code,前端字典翻译富文本)
      /// daysSince 用锚点 signalOccurredAt 实时重算 → 跟治疗链断口(同请求 server 时刻)精确一致,
      /// 不再随天数陈旧("388/389"漂移);旧 plan 无锚点 → 原样回落 baked。
      signals: applyLiveDays(
        r.signals as { daysSince?: number; signalOccurredAt?: string | null } | null,
      ),
    })),
  };
}

function serializePersona(persona: {
  id: string;
  version: number;
  computedAt: Date;
  features: Array<{ id: string; key: string; description: string; score: number | null; data: unknown }>;
}) {
  return {
    id: persona.id,
    version: persona.version,
    computedAt: persona.computedAt.toISOString(),
    features: persona.features.map((f) => ({
      id: f.id,
      key: f.key,
      description: f.description,
      score: f.score,
      /// 结构化 payload(如 entitlement_status 的 {commercialInsured, commercialInsurers, medicalInsured...})
      data: f.data ?? null,
    })),
  };
}

/**
 * W4 新加:PlanScript serializer + markdown → sections 反 parse。
 *
 * 后端写库时 plan-script.orchestrator.renderMarkdown 把 4 段拼成:
 *   > 患者:xxx · 语气:xxx
 *   ## 开场白\n{opening}\n## 告知应治未治\n{informMissed}\n## 复查建议\n{reviewAdvice}\n## 结束回访语\n{closing}
 * 这里反向用 regex 按 H2 标题切回 4 段,前端拿到的 sections shape 跟 mockScript 完全一致。
 *
 * 设计决策:**前端单一消费 sections 接口**(mock / 真实 / 流式三路同 shape),
 * 反 parse 集中在后端做,避免前端多套渲染分支。
 */
function serializeScript(s: {
  id: string;
  content: string | null;
  source: string | null;
  status: string;
  generatedAt?: Date | null;
  updatedAt: Date;
}) {
  const sections = parseScriptMarkdownToSections(s.content ?? '');
  return {
    id: s.id,
    status: s.status,
    source: (s.source as 'agent' | 'template_fallback' | 'manual' | null) ?? 'agent',
    generatedAt: (s.generatedAt ?? s.updatedAt).toISOString(),
    sections,
  };
}

const SECTION_HEAD_TO_ID: Record<string, 'opening' | 'informMissed' | 'reviewAdvice' | 'closing'> = {
  开场白: 'opening',
  告知应治未治: 'informMissed',
  复查建议: 'reviewAdvice',
  结束回访语: 'closing',
};
const SECTION_META: Record<
  'opening' | 'informMissed' | 'reviewAdvice' | 'closing',
  { label: string; durationHint: string }
> = {
  opening: { label: '开场白', durationHint: '' },
  informMissed: { label: '告知应治未治', durationHint: '' },
  reviewAdvice: { label: '复查建议', durationHint: '' },
  closing: { label: '结束回访语', durationHint: '' },
};

function parseScriptMarkdownToSections(md: string) {
  // 按 H2 切分:`^## (xxx)$` 之后到下一个 H2 之间为内容
  const ids: Array<'opening' | 'informMissed' | 'reviewAdvice' | 'closing'> = [
    'opening',
    'informMissed',
    'reviewAdvice',
    'closing',
  ];
  const result = ids.map((id) => ({
    id,
    label: SECTION_META[id].label,
    durationHint: SECTION_META[id].durationHint,
    markdown: '',
  }));
  if (!md) return result;

  const lines = md.split('\n');
  let currentId: 'opening' | 'informMissed' | 'reviewAdvice' | 'closing' | null = null;
  const buf: Record<string, string[]> = {};
  for (const line of lines) {
    const m = /^##\s+(.+?)\s*$/.exec(line);
    if (m) {
      const head = (m[1] ?? '').trim();
      const matched = SECTION_HEAD_TO_ID[head];
      currentId = matched ?? null;
      if (currentId) buf[currentId] = [];
      continue;
    }
    if (currentId) {
      buf[currentId] = buf[currentId] ?? [];
      buf[currentId]!.push(line);
    }
  }
  for (const sec of result) {
    sec.markdown = (buf[sec.id] ?? []).join('\n').trim();
  }
  return result;
}

function serializeFact(f: {
  id: string;
  type: string;
  kind: string;
  status: string;
  occurredAt: Date | null;
  plannedFor: Date | null;
  clinicId: string | null;
  title: string | null;
  summary: string | null;
  content: Prisma.JsonValue;
}) {
  return {
    id: f.id,
    type: f.type,
    kind: f.kind,
    status: f.status,
    occurredAt: f.occurredAt?.toISOString() ?? null,
    // planned 类(预约/计划治疗)时间在 plannedFor;透传给前端时间轴排序兜底
    plannedFor: f.plannedFor?.toISOString() ?? null,
    clinicId: f.clinicId,
    title: f.title,
    summary: f.summary,
    content: f.content,
  };
}

// ─────────────────────────────────────────────
// 小工具
// ─────────────────────────────────────────────
/// payment/recharge/refund.content 实际字段是 `amount_cents`(parser 写库时归一为 cents int);
/// 老代码读 `c.amount` 是字段名错配,导致 LTV 永远 0(实测路遥 ¥58 显示成 0)。W3 末修正。
function sumAmount(facts: Array<{ content: Prisma.JsonValue }>): number {
  let total = 0;
  for (const f of facts) {
    const c = f.content as Record<string, unknown>;
    total += Number(c?.amount_cents ?? c?.amount ?? 0);
  }
  return total;
}

function calcAge(birthDate: Date): number {
  const now = new Date();
  let age = now.getFullYear() - birthDate.getFullYear();
  const m = now.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && now.getDate() < birthDate.getDate())) age--;
  return age;
}
