import { Injectable, Logger } from '@nestjs/common';
import type { z } from 'zod';
import { CanonicalResourceSchemas, type CanonicalResourceKey } from '@pac/types';
import type { AssemblerConfig, EmitsConfig } from './assembler.schema';
import {
  ContractDriftError,
  normalizeCanonical,
  validateCanonical,
  type NormalizeContext,
} from './field-mapper';

/**
 * AssemblerEngine — yaml 驱动的"host raw 多表 → PAC canonical resource 数组"通用引擎。
 *
 * 三入口(ColdImport / Pull / Push)共用 — 入口只负责"如何拿到 raw tables",
 * AssemblerEngine 把 raw tables 投影成 canonical;再走 Synthesizer + Parser。
 *
 * 流程(对单个 AssemblerConfig):
 *   ① 读 primary 主表的每一行作为基础
 *   ② 按 field_mapping 重命名主表字段(canonical key ← host field)
 *   ③ 按 enum_mapping 翻译主表字段的编码值(A01 → endodontic)
 *   ④ 按 join_arrays 关联子表(1:N),把子表行按 foreign_key=主键聚合成数组,
 *      数组元素也走 field_mapping + enum_mapping
 *   ⑤ FieldMapper.normalize:yuan→cents / 时区 / boolean / gender
 *   ⑥ zod canonical schema 校验 → drift 抛 ContractDriftError
 *
 * 失败策略:单行失败计 stats.failed,日志带 [drift] 前缀;不阻塞同批其他行。
 *
 * 边界(yaml 表达不了的部分):
 *   - 多层嵌套(数组里又嵌数组)— 当前不支持,需扩
 *   - 复杂业务推导(NLP 抽取等)— 留 `custom_extractor` 钩子(本期未实现)
 *   - 自表关联 / 递归 — 罕见,真碰到再做
 */
@Injectable()
export class AssemblerEngine {
  private readonly logger = new Logger(AssemblerEngine.name);

  /**
   * 跑一份 yaml,产出 canonical 资源数组。
   *
   * @param tables 由入口提供的 host raw 表字典:`{ tb_encounter: [row, ...], tb_dx: [...], ... }`
   * @param config 该资源的 assembler yaml(已 zod 校验)
   * @param normalize amountUnit / timezone(整批数据共用)
   * @param hostId 用于 drift error 上下文
   * @returns 数组,每元素是已 zod 校验过的 canonical 资源对象;失败行会被
   *           记录在 result.errors,不抛(由调用方决定累计 failed)。
   */
  assemble(opts: {
    tables: Record<string, unknown[]>;
    config: AssemblerConfig;
    normalize: NormalizeContext;
    hostId: string;
  }): AssemblerResult {
    const { tables, config, normalize, hostId } = opts;
    const result: AssemblerResult = {
      canonical: config.canonical,
      emits: config.emits,
      rows: [],
      errors: [],
      stats: { fetched: 0, assembled: 0, failed: 0 },
    };

    const rawPrimaryRows = tables[config.primary.table];
    if (!rawPrimaryRows) {
      // 主表完全没提供 — 不算 drift,该 host 没这资源,跳过
      this.logger.warn(
        `Assembler[${config.canonical}]: primary table "${config.primary.table}" not in provided tables;skip`,
      );
      return result;
    }

    // ── 炸行兜底:dedup_by 配置时,按指定 key 取首行 ──
    // 详见 docs/algorithm/canonical-fact-layer.md §3.5 附录 B 第 3 条。
    const primaryRows = this.applyDedup(
      rawPrimaryRows,
      config.primary.dedup_by,
      config.canonical,
    );
    result.stats.fetched = primaryRows.length;

    // 预先按 foreign_key 把子表分组(性能:O(N) 一次扫,后续 O(1) 查)
    const childGroups: Record<string, Map<string, unknown[]>> = {};
    for (const [arrayKey, joinCfg] of Object.entries(config.join_arrays ?? {})) {
      const childRows = tables[joinCfg.table];
      if (!childRows) {
        this.logger.warn(
          `Assembler[${config.canonical}]: join table "${joinCfg.table}" not provided;skip ${arrayKey}[]`,
        );
        childGroups[arrayKey] = new Map();
        continue;
      }
      const group = new Map<string, unknown[]>();
      for (const child of childRows) {
        if (typeof child !== 'object' || child === null) continue;
        const fk = String((child as Record<string, unknown>)[joinCfg.foreign_key] ?? '');
        if (!fk) continue;
        let arr = group.get(fk);
        if (!arr) {
          arr = [];
          group.set(fk, arr);
        }
        arr.push(child);
      }
      childGroups[arrayKey] = group;
    }

    // 逐主表行投影
    for (const raw of primaryRows) {
      if (typeof raw !== 'object' || raw === null) {
        result.stats.failed++;
        result.errors.push({
          reason: 'primary row not an object',
          sample: raw,
        });
        continue;
      }
      const row = raw as Record<string, unknown>;
      const primaryKeyValue = String(row[config.primary.key] ?? '');

      // ⭐ 提取真原文 link(W3 末加,合规留底):
      // 拆出来的 fact(diagnosis/treatment 等)其 __source_row = 父 EMR 完整原文,
      // 保证 raw_payload 留底是 host 真原文,不是 transforms 中间态。
      // __source_row 缺失时(adapter 没经 transforms,或显式注入失败) fallback 用 row 自身。
      const sourceRow =
        (row['__source_row'] as Record<string, unknown> | undefined) ?? row;

      // ① + ② + ③ 主表字段投影(__source_row 不参与 mapping,不进 canonical)
      const canonical: Record<string, unknown> = {};
      for (const [canonicalKey, hostField] of Object.entries(config.field_mapping)) {
        const v = row[hostField];
        canonical[canonicalKey] =
          this.applyEnum(canonicalKey, v, config.enum_mapping);
      }

      // ④ join_arrays:嵌套子表数组
      for (const [arrayKey, joinCfg] of Object.entries(config.join_arrays ?? {})) {
        const childs = childGroups[arrayKey]?.get(primaryKeyValue) ?? [];
        canonical[arrayKey] = childs.map((c) =>
          this.projectChild(c as Record<string, unknown>, joinCfg),
        );
      }

      // DEBUG appointment status
      // ⑤ + ⑥ normalize + zod 校验
      try {
        const normalized = normalizeCanonical(
          canonical,
          config.canonical as CanonicalResourceKey,
          normalize,
        );
        // CanonicalResourceSchemas[key] 是 union of zod object types;
        // 这里运行期已知 key 但 ts 类型推断不出来,统一 cast 成宽泛 ZodType
        const schema = CanonicalResourceSchemas[
          config.canonical as CanonicalResourceKey
        ] as unknown as z.ZodType<unknown>;
        const validated = validateCanonical({
          raw: normalized,
          schema,
          hostId,
          resource: config.canonical,
        });
        result.rows.push({ canonical: validated, rawSource: sourceRow });
        result.stats.assembled++;
      } catch (err) {
        result.stats.failed++;
        if (err instanceof ContractDriftError) {
          result.errors.push({
            reason: err.message,
            issues: err.issues,
            sample: canonical,
          });
          this.logger.error(`[drift] ${config.canonical}: ${err.message}`);
        } else {
          result.errors.push({
            reason: err instanceof Error ? err.message : String(err),
            sample: canonical,
          });
          this.logger.error(
            `Assembler[${config.canonical}] row failed: ${
              err instanceof Error ? err.message : String(err)
            }`,
          );
        }
      }
    }

    return result;
  }

  // ─────────────────────────────────────────────────────────
  // 内部:炸行兜底
  // ─────────────────────────────────────────────────────────

  /**
   * 按 dedup_by 字段对主表行 dedup,同 key 取首行。
   *
   * 典型场景(瑞尔 DW):
   *   ETL 把 fact_emr_treatment_out 1:N JOIN 影像子表时没 groupArray,
   *   导致 1 份 EMR 有 59 张影像 → 表里出现 59 行(每行 file_url 不同,其他列重复)。
   *   yaml 配 dedup_by=id 后,只保留首行;丢失的影像信息由专门的 image yaml 投影补回。
   */
  private applyDedup(
    rows: unknown[],
    dedupBy: string | undefined,
    canonical: string,
  ): unknown[] {
    if (!dedupBy) return rows;
    const seen = new Set<string>();
    const result: unknown[] = [];
    let dropped = 0;
    for (const row of rows) {
      if (typeof row !== 'object' || row === null) {
        result.push(row);
        continue;
      }
      const key = String((row as Record<string, unknown>)[dedupBy] ?? '');
      if (!key) {
        result.push(row); // 空 key 不参与 dedup,后续 sanity check 会捕获
        continue;
      }
      if (seen.has(key)) {
        dropped++;
        continue;
      }
      seen.add(key);
      result.push(row);
    }
    if (dropped > 0) {
      this.logger.warn(
        `Assembler[${canonical}]: dedup_by="${dedupBy}" dropped ${dropped} 行炸行重复 ` +
          `(${rows.length} → ${result.length})`,
      );
    }
    return result;
  }

  // ─────────────────────────────────────────────────────────
  // 内部:子表行投影
  // ─────────────────────────────────────────────────────────

  private projectChild(
    childRaw: Record<string, unknown>,
    cfg: { field_mapping: Record<string, string>; enum_mapping?: Record<string, Record<string, string>> },
  ): Record<string, unknown> {
    const elem: Record<string, unknown> = {};
    for (const [canonicalKey, hostField] of Object.entries(cfg.field_mapping)) {
      const v = childRaw[hostField];
      elem[canonicalKey] = this.applyEnum(canonicalKey, v, cfg.enum_mapping);
    }
    return elem;
  }

  /**
   * 应用 enum_mapping:host 编码值 → PAC 枚举字符串
   *
   * 查表顺序:
   *   1. 精确匹配 fieldMap[key]
   *   2. 特殊键 fieldMap._default(兜底,长尾值统一映射)
   *   3. 原值透传(让 zod 决定是否合法)
   *
   * 典型 _default 场景:treatName 21k distinct,enum_mapping 只能 cover top N,
   *                    剩余长尾用 `_default: unknown` 统一兜底,parser 见到 'unknown'
   *                    跳过(silently warn,不污染 fact)。
   */
  private applyEnum(
    canonicalKey: string,
    value: unknown,
    mapping: Record<string, Record<string, string>> | undefined,
  ): unknown {
    if (!mapping) return value;
    const fieldMap = mapping[canonicalKey];
    if (!fieldMap) return value;

    // ⭐ W4 末:支持 array 字段(如 treat_stages = ["开髓","根备"] → ["pulp_extirpation","canal_preparation"])
    if (Array.isArray(value)) {
      const out: string[] = [];
      for (const elem of value) {
        if (typeof elem !== 'string' && typeof elem !== 'number') continue;
        const key = String(elem);
        const mapped = fieldMap[key];
        const v = mapped !== undefined ? mapped : fieldMap['_default'];
        // 空字符串 = 不进 PAC enum(host 长尾值映射不出,丢)
        if (v && v !== '') out.push(v);
      }
      return out;
    }

    if (typeof value !== 'string' && typeof value !== 'number') return value;
    const key = String(value);
    const mapped = fieldMap[key];
    if (mapped !== undefined) return mapped;
    const defaultV = fieldMap['_default'];
    return defaultV !== undefined ? defaultV : value;
  }
}

// ─────────────────────────────────────────────────────────
// Types
// ─────────────────────────────────────────────────────────

export interface AssemblerResult {
  canonical: string;
  /// v2.1:emits 跟随 result 传给 Synthesizer(yaml 缺 emits 时为 undefined,只 patient 主档允许)
  emits: EmitsConfig | undefined;
  /// rawSource = host 真原文(从 Layer A.5 transforms 注入的 __source_row 提取;
  /// 拆出来的 fact 该字段是父 EMR 完整原文,合规留底用)
  rows: Array<{ canonical: unknown; rawSource: Record<string, unknown> }>;
  errors: Array<{
    reason: string;
    issues?: unknown;
    sample?: unknown;
  }>;
  stats: {
    fetched: number;
    assembled: number;
    failed: number;
  };
}
