import fs from "fs";
import path from "path";
import {
  ARCH_EXPORT_2020_DIR,
  FEATURE_ASSISTANT_ANTI_GENERIC_RANKING_GUARD_V1,
  FEATURE_ASSISTANT_BROAD_GUARD_V1,
  FEATURE_ASSISTANT_MIN_EVIDENCE_GATE_V1
} from "../config";

interface SnapshotLink {
  relation: string;
  target_entity: string;
  target_id: string;
  source_field: string;
}

interface SnapshotRecord {
  problem_flags?: string[];
  unknown_link_count?: number;
  source_entity: string;
  source_id: string;
  display_name: string;
  attributes: Record<string, unknown>;
  links: SnapshotLink[];
}

interface SnapshotFilePayload {
  records?: unknown;
}

interface RawRetrievalResult {
  status: "ok" | "empty" | "partial" | "error";
  result_type: "list" | "summary" | "object" | "chain" | "ranking";
  items: Array<Record<string, unknown>>;
  summary: Record<string, unknown>;
  evidence: Array<Record<string, unknown>>;
  why_included: string[];
  selection_reason: string[];
  risk_factors: string[];
  business_interpretation: string[];
  confidence: "high" | "medium" | "low";
  limitations: string[];
  errors: string[];
}

type BroadnessLevel = "low" | "medium" | "high";

interface BroadQueryAssessment {
  broad_query_detected: boolean;
  broadness_level: BroadnessLevel;
  scope_confidence_hint: "low" | "medium" | "high";
  narrowing_strength: "weak" | "medium" | "strong";
}

interface RouteAwareEvidenceGate {
  min_evidence_items: number;
  min_result_items: number;
}

const BROAD_GENERIC_MARKERS = new RegExp(
  [
    "\\boverall\\b",
    "\\bgeneral\\b",
    "\\bgeneric\\b",
    "\\bsummary\\b",
    "\\btop\\b",
    "\\ball\\s+risks?\\b",
    "\\bshow\\s+all\\b",
    "\\bwhat\\s+is\\s+wrong\\b",
    "\\u0432\\s*\\u0446\\u0435\\u043b\\u043e\\u043c",
    "\\u043e\\u0431\\u0449(?:\\u0430\\u044f|\\u0443\\u044e)?\\s+\\u043a\\u0430\\u0440\\u0442\\u0438\\u043d\\u0443?",
    "\\u043e\\u0431\\u0437\\u043e\\u0440",
    "\\u043f\\u043e\\u043a\\u0430\\u0436\\u0438\\s+\\u0432\\u0441\\u0435",
    "\\u0432\\u0441\\u0435\\s+\\u0440\\u0438\\u0441\\u043a\\u0438",
    "\\u043e\\u0431\\u0449\\u0438\\u0435\\s+\\u0440\\u0438\\u0441\\u043a\\u0438",
    "\\u0442\\u043e\\u043f\\s+\\u0440\\u0438\\u0441\\u043a\\u043e\\u0432",
    "\\u0433\\u0434\\u0435\\s+\\u043f\\u0440\\u043e\\u0431\\u043b\\u0435\\u043c\\u044b",
    "\\u0447\\u0442\\u043e\\s+\\u043d\\u0435\\s+\\u0442\\u0430\\u043a"
  ].join("|"),
  "iu"
);
const ACCOUNT_SPECIFIC_MARKERS =
  /(?:\u0441\u0447\u0435\u0442(?:\u0430|\u0443|\u043e\u043c)?|account)\s*[:#]?\s*\d{2}(?:\.\d{2})?/iu;
const PERIOD_MARKERS = /\b20\d{2}(?:[-./](?:0[1-9]|1[0-2]))?\b/;
const ENTITY_SPECIFIC_MARKERS =
  /(?:\u043a\u043e\u043d\u0442\u0440\u0430\u0433\u0435\u043d\u0442|supplier|buyer|\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442|invoice|posting|register|guid|id[:=\s])/iu;
const EXACT_OBJECT_MARKERS =
  /(?:\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\s*(?:#|\u2116)|\bref\b|\bid\b|trx-\d+|inv-\d+)/iu;

const ROUTE_MIN_EVIDENCE_GATE: Record<string, RouteAwareEvidenceGate> = {
  hybrid_store_plus_live: {
    min_evidence_items: 3,
    min_result_items: 2
  },
  store_feature_risk: {
    min_evidence_items: 2,
    min_result_items: 2
  },
  batch_refresh_then_store: {
    min_evidence_items: 6,
    min_result_items: 8
  },
  store_canonical: {
    min_evidence_items: 2,
    min_result_items: 3
  },
  live_mcp_drilldown: {
    min_evidence_items: 1,
    min_result_items: 1
  }
};

function pushUniqueLine(target: string[], line: string): void {
  if (!target.includes(line)) {
    target.push(line);
  }
}

function detectBroadQuery(fragmentText: string, route: string): BroadQueryAssessment {
  const text = String(fragmentText ?? "").trim();
  const lower = text.toLowerCase();
  const tokenCount = lower.split(/\s+/).filter(Boolean).length;
  const hasGenericMarker = BROAD_GENERIC_MARKERS.test(lower);
  const hasAccountAnchor = ACCOUNT_SPECIFIC_MARKERS.test(lower);
  const hasPeriodAnchor = PERIOD_MARKERS.test(lower);
  const hasEntityAnchor = ENTITY_SPECIFIC_MARKERS.test(lower);
  const hasExactObjectAnchor = EXACT_OBJECT_MARKERS.test(lower);
  const hasGuidAnchor = extractGuids(lower).length > 0;

  let anchorScore = 0;
  if (hasGuidAnchor) anchorScore += 3;
  if (hasAccountAnchor) anchorScore += 2;
  if (hasPeriodAnchor) anchorScore += 1;
  if (hasEntityAnchor) anchorScore += 1;
  if (hasExactObjectAnchor) anchorScore += 1;

  const weakAnchors = anchorScore <= 1;
  const strongFocus = hasGuidAnchor || (hasAccountAnchor && hasPeriodAnchor) || anchorScore >= 4;
  const routeSensitiveBroad = route === "batch_refresh_then_store" || route === "hybrid_store_plus_live";

  let broadnessLevel: BroadnessLevel = "low";
  if (hasGenericMarker && !strongFocus && (weakAnchors || routeSensitiveBroad)) {
    broadnessLevel = "high";
  } else if (hasGenericMarker && !strongFocus) {
    broadnessLevel = "medium";
  }

  return {
    broad_query_detected: broadnessLevel !== "low",
    broadness_level: broadnessLevel,
    scope_confidence_hint: broadnessLevel === "high" ? "low" : broadnessLevel === "medium" ? "medium" : "high",
    narrowing_strength: anchorScore >= 3 ? "strong" : anchorScore === 2 ? "medium" : "weak"
  };
}

function enforceBroadQueryGuards(route: string, fragmentText: string, raw: RawRetrievalResult): RawRetrievalResult {
  if (!FEATURE_ASSISTANT_BROAD_GUARD_V1) {
    return raw;
  }

  const assessed = detectBroadQuery(fragmentText, route);
  const summary: Record<string, unknown> = {
    ...(raw.summary ?? {})
  };

  let antiGenericGuardApplied = false;
  let minimumEvidenceFailed = false;

  if (FEATURE_ASSISTANT_ANTI_GENERIC_RANKING_GUARD_V1 && assessed.broad_query_detected && route === "batch_refresh_then_store") {
    antiGenericGuardApplied = true;
    raw.items = raw.items.slice(0, 5);
    pushUniqueLine(raw.selection_reason, "Anti-generic ranking guard applied for broad batch request.");
    pushUniqueLine(raw.limitations, "Broad ranking output was tightened to avoid generic over-precision.");
    if (raw.confidence === "high") {
      raw.confidence = "medium";
    }
  }

  if (FEATURE_ASSISTANT_MIN_EVIDENCE_GATE_V1 && assessed.broad_query_detected && raw.status !== "error") {
    const gate = ROUTE_MIN_EVIDENCE_GATE[route];
    if (gate) {
      const broadPenalty = assessed.broadness_level === "high" ? 1 : 0;
      const requiredEvidence = gate.min_evidence_items + broadPenalty;
      const requiredItems = gate.min_result_items + broadPenalty;
      const evidenceCount = raw.evidence.length;
      const resultItemsCount = raw.items.length;
      const hasLimitedSupport = evidenceCount > 0 || resultItemsCount > 0;

      minimumEvidenceFailed = evidenceCount < requiredEvidence || resultItemsCount < requiredItems;
      if (minimumEvidenceFailed) {
        if (hasLimitedSupport) {
          raw.status = "partial";
          pushUniqueLine(raw.limitations, "Broad query support is limited; output degraded to partial confidence.");
          pushUniqueLine(raw.selection_reason, "Route-aware minimum evidence gate downgraded broad output to partial.");
          summary.degraded_to = "partial";
          if (raw.confidence === "high") {
            raw.confidence = "medium";
          }
        } else {
          raw.status = "empty";
          pushUniqueLine(raw.limitations, "Broad query lacks enough support for even a limited factual output.");
          pushUniqueLine(raw.selection_reason, "Route-aware minimum evidence gate requires clarification before retrieval output.");
          raw.confidence = "low";
          summary.degraded_to = "clarification";
        }
      }
    }
  }

  summary.broad_query_detected = assessed.broad_query_detected;
  summary.broadness_level = assessed.broadness_level;
  summary.scope_confidence_hint = assessed.scope_confidence_hint;
  summary.narrowing_strength = assessed.narrowing_strength;
  summary.broad_guard_applied = assessed.broad_query_detected;
  summary.minimum_evidence_failed = minimumEvidenceFailed;
  summary.anti_generic_guard_applied = antiGenericGuardApplied;
  summary.broad_result_flag = assessed.broad_query_detected;
  raw.summary = summary;

  return raw;
}

interface DatasetBundle {
  keyFields: SnapshotRecord[];
  problemCases: SnapshotRecord[];
  journals: SnapshotRecord[];
  ndsRegisters: SnapshotRecord[];
  docs: SnapshotRecord[];
}

function parseDateCandidate(value: unknown): number | null {
  if (typeof value !== "string") {
    return null;
  }
  const time = Date.parse(value);
  if (Number.isNaN(time)) {
    return null;
  }
  return time;
}

function extractDate(record: SnapshotRecord): string | null {
  const attrs = record.attributes ?? {};
  const directKeys = ["Period", "Date", "Р”Р°С‚Р°", "РџРµСЂРёРѕРґ", "Р”Р°С‚Р°РЎРѕР±С‹С‚РёСЏ"];
  for (const key of directKeys) {
    if (attrs[key] !== undefined && attrs[key] !== null) {
      return String(attrs[key]);
    }
  }
  for (const [key, value] of Object.entries(attrs)) {
    if (/period|date|РґР°С‚Р°|РїРµСЂРёРѕРґ/i.test(key) && typeof value === "string" && value.trim()) {
      return value;
    }
  }
  return null;
}

function countZeroGuidValues(record: SnapshotRecord): number {
  const attrs = record.attributes ?? {};
  let count = 0;
  for (const value of Object.values(attrs)) {
    if (typeof value === "string" && value.trim() === "00000000-0000-0000-0000-000000000000") {
      count += 1;
    }
  }
  return count;
}

function countNavigationLinks(record: SnapshotRecord): number {
  const attrs = record.attributes ?? {};
  let count = 0;
  for (const key of Object.keys(attrs)) {
    if (key.includes("@navigationLinkUrl")) {
      count += 1;
    }
  }
  return count;
}

function findCounterpartyLinks(record: SnapshotRecord): SnapshotLink[] {
  return record.links.filter(
    (link) =>
      link.target_entity === "Counterparty" || /supplier|buyer|counterparty/i.test(link.relation) || /РїРѕСЃС‚Р°РІ|РїРѕРєСѓРї/i.test(link.source_field)
  );
}

function extractGuids(text: string): string[] {
  const matches = text.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi) ?? [];
  return Array.from(new Set(matches.map((item) => item.toLowerCase())));
}

function hasGuidMatch(record: SnapshotRecord, guid: string): boolean {
  const source = record.source_id.toLowerCase();
  if (source.includes(guid)) {
    return true;
  }
  for (const link of record.links) {
    if (String(link.target_id ?? "").toLowerCase() === guid) {
      return true;
    }
  }
  for (const value of Object.values(record.attributes ?? {})) {
    if (typeof value === "string" && value.toLowerCase().includes(guid)) {
      return true;
    }
  }
  return false;
}

interface SemanticPeriodScope {
  from: string | null;
  to: string | null;
  granularity: "day" | "month" | "quarter" | "year" | "unknown";
}

interface SemanticRetrievalProfile {
  query_subject: string;
  account_scope: string[];
  subaccount_scope: string[];
  domain_scope: string[];
  document_types: string[];
  entity_types: string[];
  period_scope: SemanticPeriodScope;
  relation_patterns: string[];
  lifecycle_stage_filters: string[];
  anomaly_patterns: string[];
  ranking_basis: string[];
  excluded_interpretations: string[];
  explanation_focus: string[];
}

interface RecordSemanticSignals {
  account_context: string[];
  domain_scope: string[];
  document_types: string[];
  entity_types: string[];
  relation_patterns: string[];
  anomaly_patterns: string[];
  lifecycle_markers: string[];
}

interface SemanticMatchEvaluation {
  signals: RecordSemanticSignals;
  account_match: boolean;
  domain_match: boolean;
  document_match: boolean;
  entity_match: boolean;
  relation_match: boolean;
  anomaly_match: boolean;
  lifecycle_match: boolean;
  excluded_by_interpretation: boolean;
  match_score: number;
  match_reasons: string[];
  excluded_reasons: string[];
}

interface NarrowedRecordCandidate {
  record: SnapshotRecord;
  evaluation: SemanticMatchEvaluation;
}

interface HybridGroupState {
  counterparty_id: string;
  operations_count: number;
  document_ids: Set<string>;
  relations: Map<string, number>;
  samples: Array<Record<string, unknown>>;
  account_context: Set<string>;
  document_context: Set<string>;
  relation_pattern_hits: Set<string>;
  risk_factors: Set<string>;
  lifecycle_gaps: Set<string>;
  selection_reasons: Set<string>;
  total_match_score: number;
}

interface SubjectPreset {
  domains: string[];
  documents: string[];
  entities: string[];
  relations: string[];
  anomalies: string[];
}

const ACCOUNT_PRESETS: Record<string, SubjectPreset> = {
  "51": {
    domains: ["bank", "settlements", "supplier_payments"],
    documents: ["bank_statement", "payment_order", "settlement_document"],
    entities: ["counterparty", "contract", "document", "posting"],
    relations: ["payment_to_settlement", "statement_to_document", "document_to_posting"],
    anomalies: ["missing_link", "posting_mismatch", "closure_risk"]
  },
  "60": {
    domains: ["suppliers", "settlements", "supplier_payments"],
    documents: ["supplier_receipt", "payment_order", "settlement_document"],
    entities: ["counterparty", "contract", "document", "posting"],
    relations: ["payment_to_settlement", "contract_to_documents", "document_to_posting"],
    anomalies: ["missing_link", "broken_lifecycle", "closure_risk"]
  },
  "62": {
    domains: ["customers", "settlements"],
    documents: ["sales_document", "payment_order", "settlement_document"],
    entities: ["counterparty", "contract", "document", "posting"],
    relations: ["payment_to_settlement", "contract_to_documents", "document_to_posting"],
    anomalies: ["missing_link", "broken_lifecycle", "closure_risk"]
  },
  "76": {
    domains: ["other_settlements", "settlements"],
    documents: ["manual_operation", "settlement_document"],
    entities: ["counterparty", "contract", "document", "posting"],
    relations: ["contract_to_documents", "document_to_posting"],
    anomalies: ["silent_orphan", "manual_intervention_suspicion", "closure_risk"]
  },
  "97": {
    domains: ["deferred_expense", "period_close"],
    documents: ["deferred_expense_document", "manual_operation", "period_close_document"],
    entities: ["document", "posting"],
    relations: ["deferred_expense_to_writeoff", "document_to_posting"],
    anomalies: ["broken_lifecycle", "missing_link", "closure_risk"]
  },
  "01": {
    domains: ["fixed_assets"],
    documents: ["fixed_asset_card", "fixed_asset_acceptance", "depreciation_document"],
    entities: ["fixed_asset", "document", "posting"],
    relations: ["asset_card_to_depreciation", "document_to_posting"],
    anomalies: ["broken_lifecycle", "missing_link", "posting_mismatch"]
  },
  "02": {
    domains: ["fixed_assets"],
    documents: ["depreciation_document", "fixed_asset_card"],
    entities: ["fixed_asset", "document", "posting"],
    relations: ["asset_card_to_depreciation", "document_to_posting"],
    anomalies: ["broken_lifecycle", "posting_mismatch"]
  },
  "08": {
    domains: ["fixed_assets"],
    documents: ["fixed_asset_acceptance", "fixed_asset_card", "manual_operation"],
    entities: ["fixed_asset", "document", "posting"],
    relations: ["asset_card_to_depreciation", "document_to_posting"],
    anomalies: ["missing_link", "broken_lifecycle", "posting_mismatch"]
  },
  "19": {
    domains: ["vat"],
    documents: ["invoice", "vat_document", "supplier_receipt"],
    entities: ["document", "tax_entry", "counterparty"],
    relations: ["invoice_to_vat", "document_to_posting"],
    anomalies: ["missing_link", "cross_domain_inconsistency", "closure_risk"]
  },
  "68": {
    domains: ["vat", "taxes"],
    documents: ["invoice", "vat_document", "period_close_document"],
    entities: ["document", "tax_entry", "posting"],
    relations: ["invoice_to_vat", "document_to_posting"],
    anomalies: ["missing_link", "cross_domain_inconsistency", "closure_risk"]
  }
};

function uniqueStrings(items: string[]): string[] {
  return Array.from(new Set(items.map((item) => item.trim()).filter(Boolean)));
}

function pushMany(target: string[], values: string[]): void {
  for (const value of values) {
    target.push(value);
  }
}

function collectTextFromRecord(record: SnapshotRecord): string {
  const parts: string[] = [record.source_entity, record.display_name, record.source_id];
  for (const link of record.links) {
    parts.push(link.relation, link.target_entity, link.target_id, link.source_field);
  }
  for (const [key, value] of Object.entries(record.attributes ?? {})) {
    parts.push(key, String(value));
  }
  return parts.join(" ").toLowerCase();
}

function extractAccountScopeFromText(text: string): string[] {
  const matches = text.match(/\b\d{2}(?:\.\d{2})?\b/g) ?? [];
  const normalized = matches.map((item) => item.split(".")[0]);
  return uniqueStrings(normalized);
}

function inferPeriodScope(fragmentText: string): SemanticPeriodScope {
  const month = fragmentText.match(/\b(20\d{2})[-./](0[1-9]|1[0-2])\b/);
  if (month) {
    return {
      from: `${month[1]}-${month[2]}-01`,
      to: null,
      granularity: "month"
    };
  }
  const year = fragmentText.match(/\b(20\d{2})\b/);
  if (year) {
    return {
      from: `${year[1]}-01-01`,
      to: `${year[1]}-12-31`,
      granularity: "year"
    };
  }
  if (/РєРІР°СЂС‚Р°Р»|quarter/i.test(fragmentText)) {
    return {
      from: null,
      to: null,
      granularity: "quarter"
    };
  }
  if (/РјРµСЃСЏС†|month|РїРµСЂРёРѕРґ/i.test(fragmentText)) {
    return {
      from: null,
      to: null,
      granularity: "month"
    };
  }
  return {
    from: null,
    to: null,
    granularity: "unknown"
  };
}

const WRONG_DOCUMENT_MARKERS =
  /(?:\u043d\u0435\s+\u0442\u0435\u043c\s+\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442(?:\u043e\u043c)?|\u043d\u0435\s+\u0432\s+\u0442\u043e\u0442|wrong\s+document|wrong_document_type)/iu;
const REPEATED_ANOMALY_MARKERS =
  /(?:\u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449|\u0441\u0435\u0440\u0438\u0439\u043d|\u043f\u0430\u0442\u0442\u0435\u0440\u043d|repeat(?:ed|ability)?)/iu;

function inferQuerySubject(text: string, domains: string[], anomalies: string[]): string {
  const lower = text.toLowerCase();
  if ((domains.includes("bank") || domains.includes("settlements")) && WRONG_DOCUMENT_MARKERS.test(lower)) {
    return "bank_settlement_mismatch";
  }
  if (domains.includes("suppliers")) {
    return "supplier_tail_analysis";
  }
  if (domains.includes("customers")) {
    return "customer_closure_gap";
  }
  if (domains.includes("deferred_expense")) {
    return "deferred_expense_lifecycle_anomaly";
  }
  if (domains.includes("fixed_assets")) {
    return "fixed_asset_card_mismatch";
  }
  if (domains.includes("vat")) {
    return "vat_chain_conflict";
  }
  if (domains.includes("period_close")) {
    return "period_closure_risk";
  }
  if (anomalies.includes("posting_mismatch")) {
    return "document_posting_conflict";
  }
  return "cross_entity_breakage";
}

function buildSemanticRetrievalProfile(fragmentText: string): SemanticRetrievalProfile {
  const lower = fragmentText.toLowerCase();
  const accountScope = extractAccountScopeFromText(lower);
  const domainScope: string[] = [];
  const documentTypes: string[] = [];
  const entityTypes: string[] = [];
  const relationPatterns: string[] = [];
  const anomalyPatterns: string[] = [];
  const excludedInterpretations: string[] = [];
  const rankingBasis: string[] = ["closure_risk", "repeatability", "financial_impact"];
  const explanationFocus: string[] = ["why_selected", "where_chain_breaks", "what_business_risk"];

  if (/Р±Р°РЅРє|РІС‹РїРёСЃРє|СЂР°СЃС‡РµС‚РЅ|РїР»Р°С‚РµР¶|bank|payment|statement|platezh|vypisk/i.test(lower)) {
    pushMany(domainScope, ["bank", "settlements"]);
    pushMany(documentTypes, ["bank_statement", "payment_order", "settlement_document"]);
    pushMany(entityTypes, ["counterparty", "contract", "document", "posting"]);
    pushMany(relationPatterns, ["payment_to_settlement", "statement_to_document", "document_to_posting"]);
  }
  if (/РїРѕСЃС‚Р°РІ|supplier|vendor|60\b/i.test(lower)) {
    pushMany(domainScope, ["suppliers", "settlements"]);
    pushMany(documentTypes, ["supplier_receipt", "settlement_document"]);
    pushMany(entityTypes, ["counterparty", "contract", "document", "posting"]);
    pushMany(relationPatterns, ["payment_to_settlement", "contract_to_documents"]);
  }
  if (/РїРѕРєСѓРїР°С‚|customer|buyer|62\b/i.test(lower)) {
    pushMany(domainScope, ["customers", "settlements"]);
    pushMany(documentTypes, ["sales_document", "settlement_document"]);
    pushMany(entityTypes, ["counterparty", "contract", "document", "posting"]);
    pushMany(relationPatterns, ["payment_to_settlement", "contract_to_documents"]);
  }
  if (/РЅРґСЃ|vat|РєРЅРёРіР° РїРѕРєСѓРїРѕРє|РєРЅРёРіР° РїСЂРѕРґР°Р¶|СЃС‡РµС‚.?С„Р°РєС‚СѓСЂ|19\b|68\b/i.test(lower)) {
    pushMany(domainScope, ["vat", "taxes"]);
    pushMany(documentTypes, ["invoice", "vat_document"]);
    pushMany(entityTypes, ["document", "tax_entry", "posting"]);
    pushMany(relationPatterns, ["invoice_to_vat", "document_to_posting"]);
  }
  if (/РѕСЃ|РѕСЃРЅРѕРІРЅ(С‹Рµ|С‹С…)\s+СЃСЂРµРґ|fixed asset|amort|Р°РјРѕСЂС‚РёР·|01\b|02\b|08\b/i.test(lower)) {
    pushMany(domainScope, ["fixed_assets"]);
    pushMany(documentTypes, ["fixed_asset_card", "fixed_asset_acceptance", "depreciation_document"]);
    pushMany(entityTypes, ["fixed_asset", "document", "posting"]);
    pushMany(relationPatterns, ["asset_card_to_depreciation", "document_to_posting"]);
  }
  if (/СЂР±Рї|СЂР°СЃС…РѕРґС‹ Р±СѓРґСѓС‰РёС… РїРµСЂРёРѕРґРѕРІ|deferred|writeoff|97\b/i.test(lower)) {
    pushMany(domainScope, ["deferred_expense", "period_close"]);
    pushMany(documentTypes, ["deferred_expense_document", "period_close_document"]);
    pushMany(entityTypes, ["document", "posting"]);
    pushMany(relationPatterns, ["deferred_expense_to_writeoff", "document_to_posting"]);
  }
  if (/С†РµРїРѕС‡|СЂР°Р·СЂС‹РІ|СЃРІСЏР·|РґРѕРєСѓРјРµРЅС‚.*РїСЂРѕРІРѕРґ|РіРґРµ СЂРІРµС‚|Р¶РёРІСѓС‚ РѕС‚РґРµР»СЊРЅРѕ|chain|break/i.test(lower)) {
    pushMany(relationPatterns, ["document_to_posting", "contract_to_documents"]);
    pushMany(explanationFocus, ["what_conflicts_with_what", "why_not_closed"]);
  }
  if (/Р°РЅРѕРјР°Р»|СЂРёСЃРє|С…РІРѕСЃС‚|РїРѕРґРѕР·СЂ|РёСЃРєР°Р¶|suspic|risk/i.test(lower)) {
    pushMany(anomalyPatterns, ["missing_link", "broken_lifecycle", "amount_independent_risk"]);
  }
  if (WRONG_DOCUMENT_MARKERS.test(lower)) {
    pushMany(anomalyPatterns, ["wrong_document_type", "posting_mismatch", "broken_lifecycle"]);
  }
  if (/Р¶РёРІСѓС‚ РѕС‚РґРµР»СЊРЅРѕ|РЅРµ СЃРІСЏР·|Р±РµР· СЃРІСЏР·Рё|missing link/i.test(lower)) {
    pushMany(anomalyPatterns, ["missing_link", "cross_domain_inconsistency"]);
  }
  if (REPEATED_ANOMALY_MARKERS.test(lower)) {
    pushMany(anomalyPatterns, ["repeated_anomaly"]);
    pushMany(rankingBasis, ["repeatability"]);
  }
  if (/Р·Р°РєСЂС‹С‚|Р·Р°РєСЂС‹С‚РёРµ|РїРµСЂРёРѕРґ|month close|closure/i.test(lower)) {
    pushMany(domainScope, ["period_close"]);
    pushMany(anomalyPatterns, ["closure_risk", "broken_lifecycle"]);
    pushMany(documentTypes, ["period_close_document"]);
  }
  if (/РЅРµ РІ РїР»Р°С‚РµР¶Рµ|not payment/i.test(lower)) {
    pushMany(excludedInterpretations, ["simple_payment_delay"]);
  }
  if (/РЅРµ РїРѕ СЃСѓРјРј|РЅРµ СЃСѓРјРјР°|not by amount/i.test(lower)) {
    pushMany(excludedInterpretations, ["amount_only_anomaly"]);
    pushMany(rankingBasis, ["amount_independent_risk"]);
  }

  for (const account of accountScope) {
    const preset = ACCOUNT_PRESETS[account];
    if (!preset) {
      continue;
    }
    pushMany(domainScope, preset.domains);
    pushMany(documentTypes, preset.documents);
    pushMany(entityTypes, preset.entities);
    pushMany(relationPatterns, preset.relations);
    pushMany(anomalyPatterns, preset.anomalies);
  }

  if (relationPatterns.length === 0) {
    pushMany(relationPatterns, ["document_to_posting", "contract_to_documents"]);
  }
  if (anomalyPatterns.length === 0) {
    pushMany(anomalyPatterns, ["missing_link", "broken_lifecycle"]);
  }
  if (domainScope.length === 0) {
    pushMany(domainScope, ["settlements"]);
  }
  if (documentTypes.length === 0) {
    pushMany(documentTypes, ["settlement_document"]);
  }
  if (entityTypes.length === 0) {
    pushMany(entityTypes, ["counterparty", "document", "posting"]);
  }

  const dedupedDomains = uniqueStrings(domainScope);
  const dedupedAnomalies = uniqueStrings(anomalyPatterns);

  return {
    query_subject: inferQuerySubject(lower, dedupedDomains, dedupedAnomalies),
    account_scope: uniqueStrings(accountScope),
    subaccount_scope: [],
    domain_scope: dedupedDomains,
    document_types: uniqueStrings(documentTypes),
    entity_types: uniqueStrings(entityTypes),
    period_scope: inferPeriodScope(lower),
    relation_patterns: uniqueStrings(relationPatterns),
    lifecycle_stage_filters: ["created", "posted", "closed", "reconciled"],
    anomaly_patterns: dedupedAnomalies,
    ranking_basis: uniqueStrings(rankingBasis),
    excluded_interpretations: uniqueStrings(excludedInterpretations),
    explanation_focus: uniqueStrings(explanationFocus)
  };
}

function inferAccountsFromRecord(record: SnapshotRecord, corpus: string): string[] {
  const accounts: string[] = [];
  const accountTokens = corpus.match(/\b\d{2}(?:\.\d{2})?\b/g) ?? [];
  for (const token of accountTokens) {
    accounts.push(token.split(".")[0]);
  }
  for (const key of Object.keys(record.attributes ?? {})) {
    if (/СЃС‡РµС‚Р±Р°РЅРє|СЂР°СЃС‡РµС‚РЅ.*СЃС‡РµС‚|bank account|Р±Р°РЅРєРѕРІСЃРєРёР№СЃС‡РµС‚/i.test(key)) {
      accounts.push("51");
    }
    if (/СЃС‡РµС‚СѓС‡РµС‚Р°СЂР°СЃС‡РµС‚РѕРІСЃРєРѕРЅС‚СЂР°РіРµРЅС‚РѕРј/i.test(key)) {
      accounts.push("60");
    }
    if (/СЃС‡РµС‚СѓС‡РµС‚Р°РЅРґСЃ/i.test(key)) {
      accounts.push("19");
    }
    if (/СЃСѓР±РєРѕРЅС‚РѕРґС‚/i.test(key)) {
      accounts.push("60");
    }
  }
  return uniqueStrings(accounts);
}

function inferDocumentTypesFromRecord(record: SnapshotRecord, corpus: string): string[] {
  const items: string[] = [];
  if (/Р±Р°РЅРєРѕРІСЃРєРёРµРІС‹РїРёСЃРєРё|РІС‹РїРёСЃРє|СЂР°СЃС‡РµС‚РЅРѕРіРѕСЃС‡РµС‚Р°|spisaniesraschetnogoscheta|bank/i.test(corpus)) {
    pushMany(items, ["bank_statement", "payment_order"]);
  }
  if (/РїРѕСЃС‚СѓРїР»РµРЅРёРµС‚РѕРІР°СЂРѕРІСѓСЃР»СѓРі|РїРѕСЃС‚СѓРїР»РµРЅ/i.test(corpus)) {
    items.push("supplier_receipt");
  }
  if (/СЂРµР°Р»РёР·Р°С†РёСЏС‚РѕРІР°СЂРѕРІСѓСЃР»СѓРі|СЂРµР°Р»РёР·Р°С†/i.test(corpus)) {
    items.push("sales_document");
  }
  if (/РЅРґСЃ|СЃС‡РµС‚С„Р°РєС‚СѓСЂ|РєРЅРёРіРёРїРѕРєСѓРїРѕРє|РєРЅРёРіРёРїСЂРѕРґР°Р¶/i.test(corpus)) {
    pushMany(items, ["invoice", "vat_document"]);
  }
  if (/РєРѕСЂСЂРµРєС‚РёСЂРѕРІРє|СЂСѓС‡РЅ|manual/i.test(corpus)) {
    items.push("manual_operation");
  }
  if (/Р·Р°РєСЂС‹С‚РёРµ|СЂРµРіР»Р°РјРµРЅС‚/i.test(corpus)) {
    items.push("period_close_document");
  }
  if (/РѕСЃРЅРѕРІРЅ|Р°РјРѕСЂС‚РёР·|fixed_asset/i.test(corpus)) {
    pushMany(items, ["fixed_asset_card", "depreciation_document"]);
  }
  if (/СЂР°СЃС…РѕРґС‹Р±СѓРґСѓС‰РёС…РїРµСЂРёРѕРґРѕРІ|deferred|97/.test(corpus)) {
    items.push("deferred_expense_document");
  }
  if (record.source_entity.startsWith("Document") || record.source_entity.startsWith("DocumentJournal")) {
    items.push("settlement_document");
  }
  return uniqueStrings(items);
}

function inferDomainsFromRecord(corpus: string, documentTypes: string[], record: SnapshotRecord): string[] {
  const domains: string[] = [];
  if (documentTypes.some((item) => item === "bank_statement" || item === "payment_order")) {
    pushMany(domains, ["bank", "settlements"]);
  }
  if (documentTypes.some((item) => item === "supplier_receipt")) {
    pushMany(domains, ["suppliers", "settlements"]);
  }
  if (documentTypes.some((item) => item === "sales_document")) {
    pushMany(domains, ["customers", "settlements"]);
  }
  if (documentTypes.some((item) => item === "invoice" || item === "vat_document")) {
    pushMany(domains, ["vat"]);
  }
  if (documentTypes.some((item) => item === "fixed_asset_card" || item === "depreciation_document")) {
    pushMany(domains, ["fixed_assets"]);
  }
  if (documentTypes.some((item) => item === "deferred_expense_document")) {
    pushMany(domains, ["deferred_expense", "period_close"]);
  }
  if (/Р·Р°РєСЂС‹С‚РёРµ|СЂРµРіР»Р°РјРµРЅС‚|period close/i.test(corpus)) {
    domains.push("period_close");
  }
  if (findCounterpartyLinks(record).length > 0) {
    domains.push("settlements");
  }
  return uniqueStrings(domains);
}

function inferEntityTypes(record: SnapshotRecord): string[] {
  const entities: string[] = [];
  if (record.source_entity.startsWith("Document") || record.source_entity.startsWith("DocumentJournal")) {
    entities.push("document");
  }
  if (record.source_entity.startsWith("AccumulationRegister_")) {
    entities.push("posting");
  }
  if (findCounterpartyLinks(record).length > 0) {
    entities.push("counterparty");
  }
  const corpus = collectTextFromRecord(record);
  if (/РґРѕРіРѕРІРѕСЂ|contract/i.test(corpus)) {
    entities.push("contract");
  }
  if (/РѕСЃРЅРѕРІРЅ|fixed_asset|РёРЅРІРµРЅС‚Р°СЂ/i.test(corpus)) {
    entities.push("fixed_asset");
  }
  if (/РЅРґСЃ|РєРЅРёРіРёРїРѕРєСѓРїРѕРє|РєРЅРёРіРёРїСЂРѕРґР°Р¶/i.test(corpus)) {
    entities.push("tax_entry");
  }
  return uniqueStrings(entities);
}

function inferRelationPatterns(record: SnapshotRecord, corpus: string): string[] {
  const patterns: string[] = [];
  const hasDocLinks = record.links.some((item) => item.target_entity === "Document");
  const hasCounterparty = findCounterpartyLinks(record).length > 0;

  if (hasDocLinks) {
    patterns.push("document_to_posting");
  }
  if (hasCounterparty && hasDocLinks && /РїР»Р°С‚РµР¶|bank|settlement|СЂР°СЃС‡РµС‚/i.test(corpus)) {
    patterns.push("payment_to_settlement");
  }
  if (/Р±Р°РЅРєРѕРІСЃРєРёРµРІС‹РїРёСЃРєРё|РІС‹РїРёСЃРє|statement/i.test(corpus) && hasDocLinks) {
    patterns.push("statement_to_document");
  }
  if (/РѕСЃРЅРѕРІРЅ|fixed_asset|Р°РјРѕСЂС‚РёР·/i.test(corpus)) {
    patterns.push("asset_card_to_depreciation");
  }
  if (/СЂР°СЃС…РѕРґС‹Р±СѓРґСѓС‰РёС…РїРµСЂРёРѕРґРѕРІ|97|deferred/i.test(corpus)) {
    patterns.push("deferred_expense_to_writeoff");
  }
  if (/РЅРґСЃ|СЃС‡РµС‚С„Р°РєС‚СѓСЂ|РєРЅРёРіРёРїРѕРєСѓРїРѕРє|РєРЅРёРіРёРїСЂРѕРґР°Р¶/i.test(corpus)) {
    patterns.push("invoice_to_vat");
  }
  if (/РґРѕРіРѕРІРѕСЂ|contract/i.test(corpus) && hasDocLinks) {
    patterns.push("contract_to_documents");
  }
  if (/СЃРєР»Р°Рґ|С‚РѕРІР°СЂ|РјР°С‚РµСЂРёР°Р»|receipt/i.test(corpus)) {
    patterns.push("receipt_to_stock_movement");
  }
  return uniqueStrings(patterns);
}

function inferLifecycleMarkers(record: SnapshotRecord): string[] {
  const markers: string[] = ["created"];
  if (record.attributes.Recorder && String(record.attributes.Recorder).trim()) {
    markers.push("posted");
  }
  const unknownLinks = Number(record.unknown_link_count ?? 0);
  const zeroGuidValues = countZeroGuidValues(record);
  if (unknownLinks > 0 || zeroGuidValues > 0) {
    markers.push("partially_linked");
  }
  const period = extractDate(record);
  if (period && /-30T|-31T/.test(period)) {
    markers.push("period_boundary");
  }
  if (record.links.length === 0) {
    markers.push("no_continuation");
  }
  return uniqueStrings(markers);
}

function inferAnomalyPatterns(record: SnapshotRecord, corpus: string, relationPatterns: string[], lifecycleMarkers: string[]): string[] {
  const anomalies: string[] = [];
  const hasDocLinks = record.links.some((item) => item.target_entity === "Document");
  const hasCounterparty = findCounterpartyLinks(record).length > 0;
  const unknownLinks = Number(record.unknown_link_count ?? 0);
  const zeroGuidValues = countZeroGuidValues(record);

  if (!hasCounterparty || !hasDocLinks || unknownLinks > 0) {
    anomalies.push("missing_link");
  }
  if (lifecycleMarkers.includes("partially_linked") || lifecycleMarkers.includes("no_continuation")) {
    anomalies.push("broken_lifecycle");
  }
  if (relationPatterns.includes("document_to_posting") && !record.attributes.Recorder) {
    anomalies.push("posting_mismatch");
  }
  if (/СЂСѓС‡РЅ|manual|РєРѕСЂСЂРµРєС‚РёСЂРѕРІРє/.test(corpus)) {
    anomalies.push("manual_intervention_suspicion");
  }
  if (lifecycleMarkers.includes("period_boundary") && (unknownLinks > 0 || zeroGuidValues > 0)) {
    anomalies.push("closure_risk");
  }
  if (countNavigationLinks(record) >= 3 && (unknownLinks > 0 || zeroGuidValues > 0)) {
    anomalies.push("repeated_anomaly");
  }
  if (!hasCounterparty && !hasDocLinks && zeroGuidValues > 0) {
    anomalies.push("silent_orphan");
  }
  const hasAmountSignal = Object.keys(record.attributes ?? {}).some((key) => /sum|СЃСѓРјРј|РёС‚РѕРіРѕ|amount/i.test(key));
  if (!hasAmountSignal && anomalies.length > 0) {
    anomalies.push("amount_independent_risk");
  }
  const domains = inferDomainsFromRecord(corpus, inferDocumentTypesFromRecord(record, corpus), record);
  if (domains.includes("bank") && domains.includes("vat")) {
    anomalies.push("cross_domain_inconsistency");
  }
  return uniqueStrings(anomalies);
}

function inferRecordSignals(record: SnapshotRecord): RecordSemanticSignals {
  const corpus = collectTextFromRecord(record);
  const accountContext = inferAccountsFromRecord(record, corpus);
  const documentTypes = inferDocumentTypesFromRecord(record, corpus);
  const entityTypes = inferEntityTypes(record);
  const relationPatterns = inferRelationPatterns(record, corpus);
  const lifecycleMarkers = inferLifecycleMarkers(record);
  const anomalyPatterns = inferAnomalyPatterns(record, corpus, relationPatterns, lifecycleMarkers);
  const domains = inferDomainsFromRecord(corpus, documentTypes, record);

  return {
    account_context: accountContext,
    domain_scope: domains,
    document_types: documentTypes,
    entity_types: entityTypes,
    relation_patterns: relationPatterns,
    anomaly_patterns: anomalyPatterns,
    lifecycle_markers: lifecycleMarkers
  };
}

function intersects(left: string[], right: string[]): boolean {
  if (left.length === 0) {
    return true;
  }
  const rightSet = new Set(right);
  return left.some((item) => rightSet.has(item));
}

function evaluateExcludedInterpretations(
  profile: SemanticRetrievalProfile,
  signals: RecordSemanticSignals,
  record: SnapshotRecord
): { excluded: boolean; reasons: string[] } {
  const reasons: string[] = [];
  const interpretationSet = new Set(profile.excluded_interpretations);

  if (interpretationSet.has("simple_payment_delay")) {
    const structural = ["missing_link", "broken_lifecycle", "posting_mismatch", "wrong_document_type", "closure_risk"];
    const hasStructural = signals.anomaly_patterns.some((item) => structural.includes(item));
    if (!hasStructural) {
      reasons.push("РСЃРєР»СЋС‡РµРЅРѕ РєР°Рє simple_payment_delay Р±РµР· СЃС‚СЂСѓРєС‚СѓСЂРЅРѕРіРѕ РґРµС„РµРєС‚Р°.");
    }
  }
  if (interpretationSet.has("amount_only_anomaly")) {
    const hasAmountSignal = Object.keys(record.attributes ?? {}).some((key) => /sum|СЃСѓРјРј|РёС‚РѕРіРѕ|amount/i.test(key));
    const structural = ["missing_link", "broken_lifecycle", "posting_mismatch", "cross_domain_inconsistency", "silent_orphan"];
    const hasStructural = signals.anomaly_patterns.some((item) => structural.includes(item));
    if (hasAmountSignal && !hasStructural) {
      reasons.push("РСЃРєР»СЋС‡РµРЅРѕ РєР°Рє amount-only Р°РЅРѕРјР°Р»РёСЏ Р±РµР· СЃС‚СЂСѓРєС‚СѓСЂРЅС‹С… РїСЂРёР·РЅР°РєРѕРІ.");
    }
  }

  return {
    excluded: reasons.length > 0,
    reasons
  };
}

function evaluateRecordAgainstProfile(record: SnapshotRecord, profile: SemanticRetrievalProfile): SemanticMatchEvaluation {
  const signals = inferRecordSignals(record);
  const accountMatch = intersects(profile.account_scope, signals.account_context);
  const domainMatch = intersects(profile.domain_scope, signals.domain_scope);
  const documentMatch = intersects(profile.document_types, signals.document_types);
  const entityMatch = intersects(profile.entity_types, signals.entity_types);
  const relationMatch = intersects(profile.relation_patterns, signals.relation_patterns);
  const anomalyMatch = intersects(profile.anomaly_patterns, signals.anomaly_patterns);
  const lifecycleMatch = intersects(profile.lifecycle_stage_filters, signals.lifecycle_markers);
  const excluded = evaluateExcludedInterpretations(profile, signals, record);

  const matchReasons: string[] = [];
  if (accountMatch && profile.account_scope.length > 0) {
    matchReasons.push("РЎРѕРІРїР°Р» account_scope.");
  }
  if (domainMatch && profile.domain_scope.length > 0) {
    matchReasons.push("РЎРѕРІРїР°Р» domain_scope.");
  }
  if (documentMatch && profile.document_types.length > 0) {
    matchReasons.push("РЎРѕРІРїР°Р» document_types.");
  }
  if (relationMatch && profile.relation_patterns.length > 0) {
    matchReasons.push("РЎРѕРІРїР°Р»Рё relation_patterns.");
  }
  if (anomalyMatch && profile.anomaly_patterns.length > 0) {
    matchReasons.push("РЎРѕРІРїР°Р»Рё anomaly_patterns.");
  }
  if (lifecycleMatch && profile.lifecycle_stage_filters.length > 0) {
    matchReasons.push("РЎРѕРІРїР°Р» lifecycle_stage_filters.");
  }

  const matchScore =
    (accountMatch ? 3 : 0) +
    (domainMatch ? 3 : 0) +
    (documentMatch ? 2 : 0) +
    (entityMatch ? 1 : 0) +
    (relationMatch ? 3 : 0) +
    (anomalyMatch ? 2 : 0) +
    (lifecycleMatch ? 1 : 0) +
    Math.min(2, signals.anomaly_patterns.length);

  return {
    signals,
    account_match: accountMatch,
    domain_match: domainMatch,
    document_match: documentMatch,
    entity_match: entityMatch,
    relation_match: relationMatch,
    anomaly_match: anomalyMatch,
    lifecycle_match: lifecycleMatch,
    excluded_by_interpretation: excluded.excluded,
    match_score: matchScore,
    match_reasons: matchReasons,
    excluded_reasons: excluded.reasons
  };
}

function shouldIncludeSemanticCandidate(candidate: SemanticMatchEvaluation, profile: SemanticRetrievalProfile): boolean {
  if (candidate.excluded_by_interpretation) {
    return false;
  }
  if (!candidate.account_match && profile.account_scope.length > 0) {
    return false;
  }
  if (!candidate.domain_match && profile.domain_scope.length > 0) {
    return false;
  }
  const softAxes = [
    profile.document_types.length > 0,
    profile.entity_types.length > 0,
    profile.relation_patterns.length > 0,
    profile.anomaly_patterns.length > 0
  ].filter(Boolean).length;
  const softHits = [candidate.document_match, candidate.entity_match, candidate.relation_match, candidate.anomaly_match].filter(Boolean).length;
  const requiredSoftHits = softAxes > 0 ? 1 : 0;
  return softHits >= requiredSoftHits;
}

function semanticNarrowCandidates(records: SnapshotRecord[], profile: SemanticRetrievalProfile): NarrowedRecordCandidate[] {
  const evaluated = records.map((record) => ({
    record,
    evaluation: evaluateRecordAgainstProfile(record, profile)
  }));

  let narrowed = evaluated.filter((item) => shouldIncludeSemanticCandidate(item.evaluation, profile));
  if (narrowed.length > 0) {
    return narrowed;
  }

  narrowed = evaluated
    .filter(
      (item) =>
        !item.evaluation.excluded_by_interpretation &&
        (item.evaluation.account_match || item.evaluation.domain_match || item.evaluation.relation_match)
    )
    .sort((left, right) => right.evaluation.match_score - left.evaluation.match_score)
    .slice(0, Math.max(10, Math.floor(records.length * 0.35)));

  if (narrowed.length > 0) {
    return narrowed;
  }

  return evaluated
    .filter((item) => !item.evaluation.excluded_by_interpretation)
    .sort((left, right) => right.evaluation.match_score - left.evaluation.match_score)
    .slice(0, Math.max(8, Math.floor(records.length * 0.2)));
}

function toSnapshotRecords(payload: unknown): SnapshotRecord[] {
  if (!payload || typeof payload !== "object") {
    return [];
  }
  const data = payload as SnapshotFilePayload;
  if (!Array.isArray(data.records)) {
    return [];
  }

  const records: SnapshotRecord[] = [];
  for (const item of data.records) {
    if (!item || typeof item !== "object") {
      continue;
    }
    const source = item as Record<string, unknown>;
    if (typeof source.source_entity !== "string" || typeof source.source_id !== "string") {
      continue;
    }
    const links = Array.isArray(source.links)
      ? source.links
          .map((link) => {
            if (!link || typeof link !== "object") return null;
            const value = link as Record<string, unknown>;
            return {
              relation: String(value.relation ?? ""),
              target_entity: String(value.target_entity ?? ""),
              target_id: String(value.target_id ?? ""),
              source_field: String(value.source_field ?? "")
            } satisfies SnapshotLink;
          })
          .filter((link): link is SnapshotLink => link !== null)
      : [];

    records.push({
      problem_flags: Array.isArray(source.problem_flags) ? source.problem_flags.map((item) => String(item)) : [],
      unknown_link_count: typeof source.unknown_link_count === "number" ? source.unknown_link_count : 0,
      source_entity: source.source_entity,
      source_id: source.source_id,
      display_name: typeof source.display_name === "string" ? source.display_name : source.source_id,
      attributes:
        source.attributes && typeof source.attributes === "object" && !Array.isArray(source.attributes)
          ? (source.attributes as Record<string, unknown>)
          : {},
      links
    });
  }
  return records;
}

export class AssistantDataLayer {
  private cache: DatasetBundle | null = null;

  constructor(private readonly rootDir: string = ARCH_EXPORT_2020_DIR) {}

  public executeRoute(route: string, fragmentText: string): RawRetrievalResult {
    const data = this.ensureData();
    if (!data) {
      return {
        status: "error",
        result_type: "summary",
        items: [],
        summary: {},
        evidence: [],
        why_included: [],
        selection_reason: [],
        risk_factors: [],
        business_interpretation: [],
        confidence: "low",
        limitations: ["Snapshot data files could not be loaded."],
        errors: ["РЎР»РѕР№ РґР°РЅРЅС‹С… РЅРµРґРѕСЃС‚СѓРїРµРЅ: РЅРµ СѓРґР°Р»РѕСЃСЊ Р·Р°РіСЂСѓР·РёС‚СЊ snapshot-С„Р°Р№Р»С‹."]
      };
    }
    let result: RawRetrievalResult | null = null;

    if (route === "hybrid_store_plus_live") {
      result = this.executeHybrid(fragmentText, data);
    } else if (route === "store_feature_risk") {
      result = this.executeRisk(fragmentText, data);
    } else if (route === "batch_refresh_then_store") {
      result = this.executeBatch(data);
    } else if (route === "store_canonical") {
      result = this.executeCanonical(data);
    } else if (route === "live_mcp_drilldown") {
      result = this.executeDrilldown(fragmentText, data);
    }

    if (!result) {
      return {
        status: "error",
        result_type: "summary",
        items: [],
        summary: {},
        evidence: [],
        why_included: [],
        selection_reason: [],
        risk_factors: [],
        business_interpretation: [],
        confidence: "low",
        limitations: ["Route is not implemented in current data executor."],
        errors: [`Маршрут ${route} не поддержан в текущем исполнителе.`]
      };
    }

    return enforceBroadQueryGuards(route, fragmentText, result);
  }

  private ensureData(): DatasetBundle | null {
    if (this.cache) {
      return this.cache;
    }
    try {
      const keyFields = this.readRecords("09_samples_key_fields_Recorder_Ref_Supplier_Buyer_Responsible.json");
      const problemCases = this.readRecords("03_snapshot_fragment_problem_cases.json");
      const journals = this.readRecords("07_samples_DocumentJournals.json");
      const ndsRegisters = this.readRecords("08_samples_NDS_registers.json");
      const docs = [
        ...this.readRecords("04_samples_SpisanieSRaschetnogoScheta.json"),
        ...this.readRecords("05_samples_RealizaciyaTovarovUslug.json"),
        ...this.readRecords("06_samples_PostuplenieTovarovUslug.json")
      ];
      this.cache = {
        keyFields,
        problemCases,
        journals,
        ndsRegisters,
        docs
      };
      return this.cache;
    } catch {
      return null;
    }
  }

  private readRecords(fileName: string): SnapshotRecord[] {
    const fullPath = path.resolve(this.rootDir, fileName);
    if (!fs.existsSync(fullPath)) {
      return [];
    }
    const raw = fs.readFileSync(fullPath, "utf-8");
    const parsed = JSON.parse(raw) as unknown;
    return toSnapshotRecords(parsed);
  }

  private executeHybrid(fragmentText: string, data: DatasetBundle): RawRetrievalResult {
    const guidFilter = extractGuids(fragmentText);
    const semanticProfile = buildSemanticRetrievalProfile(fragmentText);
    const sourceRecords = [...data.keyFields, ...data.journals, ...data.docs];
    const narrowedCandidates: NarrowedRecordCandidate[] =
      guidFilter.length > 0
        ? sourceRecords
            .filter((record) => guidFilter.some((guid) => hasGuidMatch(record, guid)))
            .map((record) => ({
              record,
              evaluation: evaluateRecordAgainstProfile(record, semanticProfile)
            }))
        : semanticNarrowCandidates(sourceRecords, semanticProfile);
    const filtered = narrowedCandidates.map((item) => item.record);
    const semanticNarrowingApplied = guidFilter.length === 0;

    const groups = new Map<string, HybridGroupState>();

    for (const candidate of narrowedCandidates) {
      const { record, evaluation } = candidate;
      const cpLinks = findCounterpartyLinks(record);
      if (cpLinks.length === 0) {
        continue;
      }
      const docLinks = record.links.filter((link) => link.target_entity === "Document");
      for (const link of cpLinks) {
        const key = link.target_id || `unknown:${record.source_id}`;
        let group = groups.get(key);
        if (!group) {
          group = {
            counterparty_id: key,
            operations_count: 0,
            document_ids: new Set<string>(),
            relations: new Map<string, number>(),
            samples: [],
            account_context: new Set<string>(),
            document_context: new Set<string>(),
            relation_pattern_hits: new Set<string>(),
            risk_factors: new Set<string>(),
            lifecycle_gaps: new Set<string>(),
            selection_reasons: new Set<string>(),
            total_match_score: 0
          };
          groups.set(key, group);
        }
        group.operations_count += 1;
        group.total_match_score += evaluation.match_score;
        for (const relation of [link.relation, ...docLinks.map((item) => item.relation)]) {
          group.relations.set(relation, (group.relations.get(relation) ?? 0) + 1);
        }
        for (const account of evaluation.signals.account_context) {
          if (semanticProfile.account_scope.length === 0 || semanticProfile.account_scope.includes(account)) {
            group.account_context.add(account);
          }
        }
        for (const item of evaluation.signals.document_types) {
          group.document_context.add(item);
        }
        for (const item of evaluation.signals.relation_patterns) {
          group.relation_pattern_hits.add(item);
        }
        for (const item of evaluation.signals.anomaly_patterns) {
          group.risk_factors.add(item);
        }
        for (const item of evaluation.signals.lifecycle_markers) {
          if (item === "partially_linked" || item === "no_continuation" || item === "period_boundary") {
            group.lifecycle_gaps.add(item);
          }
        }
        for (const reason of evaluation.match_reasons.slice(0, 4)) {
          group.selection_reasons.add(reason);
        }
        for (const docLink of docLinks) {
          if (docLink.target_id) {
            group.document_ids.add(docLink.target_id);
          }
        }
        if (group.samples.length < 3) {
          const unknownLinks = Number(record.unknown_link_count ?? 0);
          group.samples.push({
            source_entity: record.source_entity,
            source_id: record.source_id,
            period: extractDate(record),
            recorder: record.attributes.Recorder ?? null,
            account_context: evaluation.signals.account_context,
            document_context: evaluation.signals.document_types,
            relation_patterns: evaluation.signals.relation_patterns,
            anomaly_patterns: evaluation.signals.anomaly_patterns,
            lifecycle_markers: evaluation.signals.lifecycle_markers,
            missing_links: unknownLinks
          });
        }
      }
    }

    const items = Array.from(groups.values())
      .map((group) => ({
        entity_type: "counterparty",
        entity_id: group.counterparty_id,
        label: group.counterparty_id,
        counterparty_id: group.counterparty_id,
        operations_count: group.operations_count,
        document_refs_count: group.document_ids.size,
        account_context: Array.from(group.account_context),
        document_context: Array.from(group.document_context),
        relation_pattern_hits: Array.from(group.relation_pattern_hits),
        risk_factors: Array.from(group.risk_factors),
        lifecycle_gaps: Array.from(group.lifecycle_gaps),
        selection_reason: Array.from(group.selection_reasons).slice(0, 6),
        ranking_score: Number(
          (
            group.operations_count +
            group.risk_factors.size * 2 +
            group.relation_pattern_hits.size * 1.5 +
            group.lifecycle_gaps.size * 1.25 +
            group.total_match_score / Math.max(1, group.operations_count)
          ).toFixed(2)
        ),
        confidence:
          group.risk_factors.size >= 2 || group.relation_pattern_hits.size >= 2
            ? "high"
            : group.risk_factors.size >= 1
              ? "medium"
              : "low",
        business_interpretation:
          group.risk_factors.size > 0
            ? "Р•СЃС‚СЊ РїСЂРёР·РЅР°РєРё СЂР°Р·СЂС‹РІР° СЂР°СЃС‡РµС‚РЅРѕР№ С†РµРїРѕС‡РєРё: С‡Р°СЃС‚СЊ СЃРІСЏР·РµР№/СЌС‚Р°РїРѕРІ lifecycle РїРѕРґС‚РІРµСЂР¶РґРµРЅР° РЅРµРїРѕР»РЅРѕ."
            : "Р•СЃС‚СЊ СЃРІСЏР·Р°РЅРЅР°СЏ РѕРїРµСЂР°С†РёРѕРЅРЅР°СЏ С†РµРїРѕС‡РєР°, РЅРѕ СЏРІРЅС‹Рµ СЂРёСЃРє-РїР°С‚С‚РµСЂРЅС‹ РІС‹СЂР°Р¶РµРЅС‹ СЃР»Р°Р±Рѕ.",
        relation_types: Array.from(group.relations.entries())
          .sort((left, right) => right[1] - left[1])
          .map((item) => item[0]),
        samples: group.samples,
        evidence_pack: group.samples.slice(0, 2)
      }))
      .sort((left, right) => {
        const scoreDiff = Number(right.ranking_score) - Number(left.ranking_score);
        if (scoreDiff !== 0) {
          return scoreDiff;
        }
        return Number(right.operations_count) - Number(left.operations_count);
      })
      .slice(0, 8)
      .map((item, index) => ({
        ...item,
        rank: index + 1
      }));

    if (items.length === 0) {
      return {
        status: "empty",
        result_type: "chain",
        items: [],
        summary: {
          source_records: sourceRecords.length,
          filtered_records_after_narrowing: filtered.length,
          checked_records: filtered.length,
          matched_counterparties: 0,
          semantic_narrowing_applied: semanticNarrowingApplied,
          guid_mode: guidFilter.length > 0,
          query_subject: semanticProfile.query_subject,
          semantic_profile: semanticProfile,
          ranking_basis: semanticProfile.ranking_basis
        },
        evidence: [],
        why_included: [],
        selection_reason: [
          "РџРѕРёСЃРє СЃС‚СЂРѕРёР»СЃСЏ РїРѕ semantic retrieval profile, РЅРѕ РїРѕРґС…РѕРґСЏС‰РёРµ РєРѕРЅС‚СЂР°РіРµРЅС‚С‹ РЅРµ РЅР°Р№РґРµРЅС‹.",
          "Р¤РёР»СЊС‚СЂР°С†РёСЏ РёСЃРїРѕР»СЊР·РѕРІР°Р»Р° РїРµСЂРµСЃРµС‡РµРЅРёРµ account/domain/document/relation/anomaly РѕРіСЂР°РЅРёС‡РµРЅРёР№.",
          guidFilter.length > 0
            ? "GUID-С„РёР»СЊС‚СЂР°С†РёСЏ РІРєР»СЋС‡РµРЅР°."
            : `GUID РѕС‚СЃСѓС‚СЃС‚РІРѕРІР°Р», РІС‹РїРѕР»РЅРµРЅРѕ semantic narrowing (${filtered.length}/${sourceRecords.length}).`
        ],
        risk_factors: semanticProfile.anomaly_patterns,
        business_interpretation: [
          "РџРѕ С‚РµРєСѓС‰РµРјСѓ РїСЂРѕС„РёР»СЋ Р·Р°РїСЂРѕСЃР° СѓСЃС‚РѕР№С‡РёРІС‹С… СЂР°Р·СЂС‹РІРѕРІ С†РµРїРѕС‡РєРё РЅРµ РѕР±РЅР°СЂСѓР¶РµРЅРѕ.",
          "Р”Р»СЏ С‚РѕС‡РµС‡РЅРѕРіРѕ drilldown РґРѕР±Р°РІСЊС‚Рµ GUID РёР»Рё СѓС‚РѕС‡РЅРёС‚Рµ РїРµСЂРёРѕРґ/РєРѕРЅС‚СЂР°РіРµРЅС‚Р°."
        ],
        confidence: "medium",
        limitations: [
          guidFilter.length > 0 ? "РџРѕРёСЃРє РѕРіСЂР°РЅРёС‡РµРЅ РїРµСЂРµРґР°РЅРЅС‹РјРё GUID." : "РџРѕРёСЃРє РІС‹РїРѕР»РЅРµРЅ РїРѕ semantic narrowing Р±РµР· GUID.",
          "РСЃС‚РѕС‡РЅРёРє РґР°РЅРЅС‹С… вЂ” snapshot 2020 (read-only), Р° РЅРµ live СЃРѕСЃС‚РѕСЏРЅРёРµ Р±Р°Р·С‹ 1РЎ."
        ],
        errors: []
      };
    }

    const evidence = items.flatMap((item) =>
      (item.samples as Array<Record<string, unknown>>).slice(0, 1).map((sample) => ({
        counterparty_id: item.counterparty_id,
        source_id: sample.source_id,
        source_entity: sample.source_entity,
        period: sample.period,
        account_context: item.account_context,
        relation_pattern_hits: item.relation_pattern_hits,
        risk_factors: item.risk_factors
      }))
    );

    const aggregatedRiskFactors = uniqueStrings(
      items.flatMap((item) => (Array.isArray(item.risk_factors) ? (item.risk_factors as string[]) : []))
    );

    return {
      status: "ok",
      result_type: "chain",
      items,
      summary: {
        source_records: sourceRecords.length,
        filtered_records_after_narrowing: filtered.length,
        checked_records: filtered.length,
        matched_counterparties: items.length,
        route_focus: "cross_entity_chain",
        semantic_narrowing_applied: semanticNarrowingApplied,
        guid_mode: guidFilter.length > 0,
        query_subject: semanticProfile.query_subject,
        semantic_profile: semanticProfile,
        ranking_basis: semanticProfile.ranking_basis
      },
      evidence: evidence.slice(0, 12),
      why_included: [
        `РЎРµРјР°РЅС‚РёС‡РµСЃРєРѕРµ СЃСѓР¶РµРЅРёРµ РІС‹РїРѕР»РЅРµРЅРѕ РїРѕ РїСЂРѕС„РёР»СЋ ${semanticProfile.query_subject}.`,
        semanticProfile.account_scope.length > 0
          ? `РЈС‡РёС‚С‹РІР°Р»РёСЃСЊ СЃС‡РµС‚Р°: ${semanticProfile.account_scope.join(", ")}.`
          : "РЎС‡РµС‚Р° РЅРµ Р±С‹Р»Рё Р·Р°РґР°РЅС‹ СЏРІРЅРѕ, РёСЃРїРѕР»СЊР·РѕРІР°РЅС‹ domain/document/relation РѕРіСЂР°РЅРёС‡РµРЅРёСЏ.",
        `РџРѕСЃР»Рµ narrowing РѕСЃС‚Р°Р»РѕСЃСЊ ${filtered.length} РёР· ${sourceRecords.length} Р·Р°РїРёСЃРµР№.`
      ],
      selection_reason: [
        "РћС‚Р±РѕСЂ РѕСЃРЅРѕРІР°РЅ РЅР° РїРµСЂРµСЃРµС‡РµРЅРёРё account_scope + domain_scope + document_types + relation_patterns + anomaly_patterns.",
        "GUID-mode РѕС‚РєР»СЋС‡РµРЅ: full scan Р±РµР· РѕРіСЂР°РЅРёС‡РёС‚РµР»РµР№ РЅРµ РёСЃРїРѕР»СЊР·РѕРІР°Р»СЃСЏ.",
        `Р Р°РЅР¶РёСЂРѕРІР°РЅРёРµ РІС‹РїРѕР»РЅРµРЅРѕ РїРѕ basis: ${semanticProfile.ranking_basis.join(", ")}.`
      ],
      risk_factors:
        aggregatedRiskFactors.length > 0
          ? aggregatedRiskFactors
          : ["Р’С‹СЃРѕРєР°СЏ РїР»РѕС‚РЅРѕСЃС‚СЊ РѕРїРµСЂР°С†РёР№ РїРѕ РєРѕРЅС‚СЂР°РіРµРЅС‚Сѓ РјРѕР¶РµС‚ СѓРєР°Р·С‹РІР°С‚СЊ РЅР° РЅРµР·Р°РєСЂС‹С‚С‹Рµ С†РµРїРѕС‡РєРё."],
      business_interpretation: [
        "Р РµР·СѓР»СЊС‚Р°С‚ РѕС‚СЂР°Р¶Р°РµС‚ РЅРµ РїСЂРѕСЃС‚Рѕ РѕР±СЉРµРј РѕРїРµСЂР°С†РёР№, Р° СЃС‚СЂСѓРєС‚СѓСЂРЅС‹Рµ РїСЂРёР·РЅР°РєРё СЂР°Р·СЂС‹РІР° С†РµРїРѕС‡РєРё Рё lifecycle-РєРѕРЅС„Р»РёРєС‚Р°.",
        "РљРѕРЅС‚СЂР°РіРµРЅС‚С‹ РІ С‚РѕРїРµ РїСЂРёРѕСЂРёС‚РµС‚РЅС‹ РґР»СЏ РїСЂРѕРІРµСЂРєРё РЅР° РЅРµРІРµСЂРЅС‹Р№ С‚РёРї Р·Р°РєСЂС‹РІР°СЋС‰РµРіРѕ РґРѕРєСѓРјРµРЅС‚Р° Рё РЅРµР·Р°РІРµСЂС€РµРЅРЅС‹Рµ СЃРІСЏР·Рё."
      ],
      confidence: "high",
      limitations: [
        guidFilter.length > 0 ? "Р’С‹Р±РѕСЂРєР° РѕРіСЂР°РЅРёС‡РµРЅР° GUID РёР· Р·Р°РїСЂРѕСЃР°." : "Р’С‹Р±РѕСЂРєР° РѕРіСЂР°РЅРёС‡РµРЅР° semantic retrieval profile.",
        "РСЃС‚РѕС‡РЅРёРє РґР°РЅРЅС‹С… вЂ” snapshot 2020 (read-only), РЅРµ live РєРѕРЅС‚СѓСЂ 1РЎ."
      ],
      errors: []
    };
  }

  private executeRisk(_fragmentText: string, data: DatasetBundle): RawRetrievalResult {
    const records = [...data.problemCases, ...data.ndsRegisters];
    const scored = records
      .map((record) => {
        const reasons: string[] = [];
        let score = 0;
        const unknownLinks = Number(record.unknown_link_count ?? 0);
        const zeroGuidValues = countZeroGuidValues(record);
        const navigationLinks = countNavigationLinks(record);
        const cpLinks = findCounterpartyLinks(record).length;

        if (unknownLinks > 0) {
          score += 3;
          reasons.push(`Р•СЃС‚СЊ СЃРІСЏР·Рё СЃ РЅРµРѕРїСЂРµРґРµР»РµРЅРЅРѕР№ СЃСѓС‰РЅРѕСЃС‚СЊСЋ (${unknownLinks}).`);
        }
        if (zeroGuidValues > 0) {
          score += Math.min(3, 1 + zeroGuidValues);
          reasons.push(`РќР°Р№РґРµРЅС‹ РЅСѓР»РµРІС‹Рµ GUID РІ РєР»СЋС‡РµРІС‹С… РїРѕР»СЏС… (${zeroGuidValues}).`);
        }
        if (navigationLinks > 0) {
          score += 1;
          reasons.push("Р•СЃС‚СЊ РЅР°РІРёРіР°С†РёРѕРЅРЅС‹Рµ СЃСЃС‹Р»РєРё, С‚СЂРµР±СѓСЋС‰РёРµ СЃРІРµСЂРєРё СЃРІСЏР·РµР№.");
        }
        if (cpLinks === 0) {
          score += 1;
          reasons.push("РќРµС‚ СЏРІРЅРѕР№ СЃРІСЏР·Рё СЃ РєРѕРЅС‚СЂР°РіРµРЅС‚РѕРј.");
        }
        const flags = Array.isArray(record.problem_flags) ? record.problem_flags : [];
        if (flags.length > 0) {
          score += 1;
        }

        return {
          source_entity: record.source_entity,
          source_id: record.source_id,
          period: extractDate(record),
          risk_score: score,
          reasons,
          unknown_link_count: unknownLinks,
          zero_guid_values: zeroGuidValues,
          navigation_links: navigationLinks
        };
      })
      .filter((item) => item.risk_score >= 2)
      .sort((left, right) => right.risk_score - left.risk_score);

    const items = scored.slice(0, 15);
    if (items.length === 0) {
      return {
        status: "empty",
        result_type: "list",
        items: [],
        summary: {
          checked_records: records.length,
          risky_records: 0
        },
        evidence: [],
        why_included: [],
        selection_reason: ["Р РёСЃРє-РѕС†РµРЅРєР° РІС‹РїРѕР»РЅСЏР»Р°СЃСЊ РїРѕ С‚РµС…РЅРёС‡РµСЃРєРёРј РїСЂРёР·РЅР°РєР°Рј, РЅРѕ Р·Р°РїРёСЃРё РІС‹С€Рµ РїРѕСЂРѕРіР° РЅРµ РЅР°Р№РґРµРЅС‹."],
        risk_factors: [],
        business_interpretation: ["РџРѕ С‚РµРєСѓС‰РµРјСѓ СЃСЂРµР·Сѓ СЏРІРЅС‹Рµ СЂРёСЃРє-РїСЂРёР·РЅР°РєРё РЅРµ РѕР±РЅР°СЂСѓР¶РµРЅС‹."],
        confidence: "medium",
        limitations: ["РћС†РµРЅРєР° РѕСЃРЅРѕРІР°РЅР° РЅР° snapshot-РґР°РЅРЅС‹С… Рё СЌРІСЂРёСЃС‚РёС‡РµСЃРєРѕРј risk score."],
        errors: []
      };
    }

    const averageScore = items.reduce((acc, item) => acc + item.risk_score, 0) / items.length;
    return {
      status: "ok",
      result_type: "list",
      items,
      summary: {
        checked_records: records.length,
        risky_records: items.length,
        average_risk_score: Number(averageScore.toFixed(2))
      },
      evidence: items.slice(0, 10).map((item) => ({
        source_entity: item.source_entity,
        source_id: item.source_id,
        risk_score: item.risk_score
      })),
      why_included: ["Р’ РѕС‚РІРµС‚ РІРєР»СЋС‡РµРЅС‹ Р·Р°РїРёСЃРё СЃ risk_score >= 2."],
      selection_reason: [
        "score СЂР°СЃС‚РµС‚ РїСЂРё unknown links, zero GUID, РЅР°РІРёРіР°С†РёРѕРЅРЅС‹С… СЃСЃС‹Р»РєР°С… Рё РѕС‚СЃСѓС‚СЃС‚РІРёРё СЏРІРЅРѕРіРѕ РєРѕРЅС‚СЂР°РіРµРЅС‚Р°."
      ],
      risk_factors: [
        "unknown_link_count",
        "zero_guid_values",
        "navigation_links",
        "missing_counterparty_link"
      ],
      business_interpretation: ["Р­С‚Рё Р·Р°РїРёСЃРё С‚СЂРµР±СѓСЋС‚ РїРµСЂРІРёС‡РЅРѕР№ Р±СѓС…РіР°Р»С‚РµСЂСЃРєРѕР№ РїСЂРѕРІРµСЂРєРё РєР°Рє РїРѕС‚РµРЅС†РёР°Р»СЊРЅС‹Рµ Р°РЅРѕРјР°Р»РёРё."],
      confidence: "high",
      limitations: ["Р РёСЃРє-С„Р°РєС‚РѕСЂС‹ РѕРїСЂРµРґРµР»СЏСЋС‚СЃСЏ СЌРІСЂРёСЃС‚РёРєРѕР№, Р° РЅРµ РїРѕР»РЅС‹Рј РЅР°Р±РѕСЂРѕРј Р±РёР·РЅРµСЃ-РїСЂР°РІРёР» 1РЎ."],
      errors: []
    };
  }

  private executeBatch(data: DatasetBundle): RawRetrievalResult {
    const source = [...data.problemCases, ...data.keyFields, ...data.docs];
    const byEntity = new Map<string, number>();
    for (const record of source) {
      byEntity.set(record.source_entity, (byEntity.get(record.source_entity) ?? 0) + 1);
    }

    const items = Array.from(byEntity.entries())
      .sort((left, right) => right[1] - left[1])
      .slice(0, 10)
      .map(([entity, count], index) => ({
        rank: index + 1,
        entity,
        records_count: count
      }));

    return {
      status: items.length > 0 ? "ok" : "empty",
      result_type: "ranking",
      items,
      summary: {
        checked_records: source.length,
        ranked_entities: items.length
      },
      evidence: items.slice(0, 5).map((item) => ({
        entity: item.entity,
        records_count: item.records_count
      })),
      why_included: items.length > 0 ? ["РџРѕРєР°Р·Р°РЅС‹ СЃСѓС‰РЅРѕСЃС‚Рё СЃ РјР°РєСЃРёРјР°Р»СЊРЅС‹Рј РєРѕР»РёС‡РµСЃС‚РІРѕРј Р·Р°РїРёСЃРµР№."] : [],
      selection_reason: ["Р Р°РЅР¶РёСЂРѕРІР°РЅРёРµ РІС‹РїРѕР»РЅРµРЅРѕ РїРѕ records_count РїРѕ СѓР±С‹РІР°РЅРёСЋ."],
      risk_factors: ["Р’С‹СЃРѕРєРёР№ РѕР±СЉРµРј Р·Р°РїРёСЃРµР№ РїРѕ СЃСѓС‰РЅРѕСЃС‚Рё РїРѕРІС‹С€Р°РµС‚ РїСЂРёРѕСЂРёС‚РµС‚ РїСЂРѕРІРµСЂРєРё."],
      business_interpretation: [
        "РЎСѓС‰РЅРѕСЃС‚Рё РІ С‚РѕРїРµ СЂР°РЅРіР° С‡Р°С‰Рµ РґР°СЋС‚ РЅР°РёР±РѕР»СЊС€РёР№ РІРєР»Р°Рґ РІ РїСЂРѕР±Р»РµРјРЅС‹Р№ РѕР±СЉРµРј Рё С‚СЂРµР±СѓСЋС‚ РїСЂРёРѕСЂРёС‚РµС‚РЅРѕРіРѕ Р°СѓРґРёС‚Р°."
      ],
      confidence: "medium",
      limitations: ["Р Р°РЅР¶РёСЂРѕРІР°РЅРёРµ РїРѕ РѕР±СЉРµРјСѓ РЅРµ РІСЃРµРіРґР° СЌРєРІРёРІР°Р»РµРЅС‚РЅРѕ Р±РёР·РЅРµСЃ-СЂРёСЃРєСѓ."],
      errors: []
    };
  }

  private executeCanonical(data: DatasetBundle): RawRetrievalResult {
    const items = data.docs
      .map((record) => {
        const period = extractDate(record);
        return {
          source_entity: record.source_entity,
          source_id: record.source_id,
          display_name: record.display_name,
          period,
          counterparty_id: findCounterpartyLinks(record)[0]?.target_id ?? null,
          recorder: record.attributes.Recorder ?? null,
          sort_key: parseDateCandidate(period) ?? 0
        };
      })
      .sort((left, right) => right.sort_key - left.sort_key)
      .slice(0, 12)
      .map(({ sort_key: _ignored, ...record }) => record);

    return {
      status: items.length > 0 ? "ok" : "empty",
      result_type: "list",
      items,
      summary: {
        checked_records: data.docs.length,
        returned_records: items.length
      },
      evidence: items.slice(0, 6).map((item) => ({
        source_entity: item.source_entity,
        source_id: item.source_id,
        period: item.period
      })),
      why_included: items.length > 0 ? ["РџРѕРєР°Р·Р°РЅС‹ РїРѕСЃР»РµРґРЅРёРµ РїРѕ РґР°С‚Рµ Р·Р°РїРёСЃРё РєР°РЅРѕРЅРёС‡РµСЃРєРѕРіРѕ РґРѕРєСѓРјРµРЅС‚РЅРѕРіРѕ СЃР»РѕСЏ."] : [],
      selection_reason: ["РћС‚Р±РѕСЂ РїРѕ РјР°РєСЃРёРјР°Р»СЊРЅРѕР№ РґР°С‚Рµ РґРѕРєСѓРјРµРЅС‚Р° РІ РїСЂРµРґРµР»Р°С… snapshot."],
      risk_factors: [],
      business_interpretation: ["РЎР»РѕР№ РѕС‚СЂР°Р¶Р°РµС‚ Р±Р°Р·РѕРІС‹Р№ factual-СЃСЂРµР· РґРѕРєСѓРјРµРЅС‚РѕРІ РґР»СЏ РѕРїРµСЂР°С‚РёРІРЅРѕР№ СЃРІРµСЂРєРё."],
      confidence: "high",
      limitations: ["Р­С‚Рѕ read-only snapshot, Р° РЅРµ РѕРЅР»Р°Р№РЅ-СЃРѕСЃС‚РѕСЏРЅРёРµ 1РЎ."],
      errors: []
    };
  }

  private executeDrilldown(fragmentText: string, data: DatasetBundle): RawRetrievalResult {
    const guidFilter = extractGuids(fragmentText);
    if (guidFilter.length === 0) {
      return {
        status: "empty",
        result_type: "object",
        items: [],
        summary: {
          reason: "guid_not_provided"
        },
        evidence: [],
        why_included: [],
        selection_reason: ["Р”Р»СЏ drilldown С‚СЂРµР±СѓРµС‚СЃСЏ GUID РІ С‚РµРєСЃС‚Рµ Р·Р°РїСЂРѕСЃР°."],
        risk_factors: [],
        business_interpretation: ["Р‘РµР· GUID С‚РѕС‡РµС‡РЅС‹Р№ drilldown РЅРµРІРѕР·РјРѕР¶РµРЅ."],
        confidence: "low",
        limitations: ["Р”РѕР±Р°РІСЊС‚Рµ GUID РґРѕРєСѓРјРµРЅС‚Р°/РѕР±СЉРµРєС‚Р° РґР»СЏ source-of-record РїСЂРѕРІРµСЂРєРё."],
        errors: []
      };
    }

    const all = [...data.keyFields, ...data.problemCases, ...data.docs, ...data.journals, ...data.ndsRegisters];
    const matches = all
      .filter((record) => guidFilter.some((guid) => hasGuidMatch(record, guid)))
      .slice(0, 20)
      .map((record) => ({
        source_entity: record.source_entity,
        source_id: record.source_id,
        display_name: record.display_name,
        period: extractDate(record),
        links_count: record.links.length
      }));

    return {
      status: matches.length > 0 ? "ok" : "empty",
      result_type: "object",
      items: matches,
      summary: {
        query_guids: guidFilter,
        matched_records: matches.length
      },
      evidence: matches.slice(0, 10),
      why_included: matches.length > 0 ? ["Р’РєР»СЋС‡РµРЅС‹ Р·Р°РїРёСЃРё, СЃРѕРґРµСЂР¶Р°С‰РёРµ GUID РёР· Р·Р°РїСЂРѕСЃР°."] : [],
      selection_reason: ["РџРѕРёСЃРє РїРѕ source_id, linked target_id Рё СЃС‚СЂРѕРєРѕРІС‹Рј Р°С‚СЂРёР±СѓС‚Р°Рј."],
      risk_factors: [],
      business_interpretation: ["Р РµР·СѓР»СЊС‚Р°С‚ РїРѕРєР°Р·С‹РІР°РµС‚ source-of-record РѕР±СЉРµРєС‚С‹ РїРѕ РїРµСЂРµРґР°РЅРЅС‹Рј РёРґРµРЅС‚РёС„РёРєР°С‚РѕСЂР°Рј."],
      confidence: matches.length > 0 ? "high" : "medium",
      limitations: ["РџРѕРёСЃРє РѕРіСЂР°РЅРёС‡РµРЅ Р»РѕРєР°Р»СЊРЅС‹Рј snapshot-РїР°РєРµС‚РѕРј."],
      errors: []
    };
  }
}


