using PlotLine.Models.Investigations; namespace PlotLine.Services.Investigations; // Private diagnostic projection only. No diagnostic fields are persisted into legacy evaluations. public static class InvestigationDiagnostics { public static object Trace(Catalogue c, string concern, IReadOnlyList answers, ISet available, int continueUntil = 0) { var e = InvestigationEngine.Evaluate(c, concern, answers, available, continueUntil); var current = answers.GroupBy(a => a.Question).ToDictionary(g => g.Key, g => g.Last()); var stop = e.NextQuestion != null ? "continue" : e.Recommendations.Count > 0 ? "supported-evidence" : e.Hypotheses.Any(h=>h.Supported)&&!e.Hypotheses.Any(h=>h.Supported&&h.Eligible) ? "capability-unavailable" : answers.Count >= c.MaxAnswers ? "maximum-questions" : e.Candidates.Count == 0 ? "no-useful-questions" : "completion"; return new { editorialSemantics = c.Editorial, catalogue = c.Version, engineVersion=c.EngineVersion, minimumAnswers=c.MinAnswers,maximumAnswers=c.MaxAnswers,continueUntil, concern, activated = c.Concerns.Single(x=>x.Id==concern).Priors, answers = answers.ToArray(), evaluation = e, stoppingCondition = stop, conclusionReasons = new { insufficientEvidence=e.Hypotheses.All(h=>!h.Supported), conflictingEvidence=e.Hypotheses.Any(h=>h.Conflicts.Length>0), noRemainingUsefulQuestions=e.Candidates.Count==0, safetyLimit=e.AnswerCount>=c.MaxAnswers, missingCapabilityMapping=c.Hypotheses.Any(h=>h.Capabilities.Count==0||h.Capabilities.Any(id=>!c.Capabilities.Any(d=>d.Id==id))), capabilityUnavailable=e.Hypotheses.Any(h=>h.Supported&&!h.Eligible), unresolvedCompetitor=e.Hypotheses.Any(h=>h.Supported&&h.Eligible)&&e.Recommendations.Count==0, configuration="Validated before evaluation; invalid imports fail explicitly rather than becoming an inconclusive diagnosis.", persistence="Evaluation is pure. Save/replay failures are handled as HTTP errors; no successful persistence is inferred from this trace." }, blockers = e.Hypotheses.Select(h=>new {h.Id, h.Missing, h.Conflicts, threshold=c.Hypotheses.Single(x=>x.Id==h.Id).Threshold, capabilities=c.Hypotheses.Single(x=>x.Id==h.Id).Capabilities.Select(id=>new {id, mapped=c.Capabilities.Any(d=>d.Id==id), eligible=available.Contains(id) && c.Capabilities.Any(d=>d.Id==id&&d.Available&&InvestigationEngine.Allows(d.Prerequisites,current))})}), questions = c.Questions.Select(q=>new {q.Id, q.Purpose, options=q.Options.Select(o=>new{o.Id,o.Text,o.Uncertain,o.Contributions}), reason = q.Retired ? "retired" : current.ContainsKey(q.Id) ? "already answered" : !InvestigationEngine.Allows(q.Eligibility,current) ? "prerequisites or exclusions" : e.Candidates.Any(x=>x.Id==q.Id) ? e.Candidates.Single(x=>x.Id==q.Id).Reason : "not useful in this state"}), independence = e.Evidence.GroupBy(v=>(v.Hypothesis,v.Dimension,v.Group)).Select(g=>new{g.Key.Hypothesis,g.Key.Dimension,g.Key.Group,questions=g.Select(v=>v.Question),positive=g.Where(v=>!v.Uncertain).Select(v=>Math.Max(0,v.Raw)).DefaultIfEmpty().Max(),negative=g.Where(v=>!v.Uncertain).Select(v=>Math.Min(0,v.Raw)).DefaultIfEmpty().Min(),redundant=g.Count(v=>v.Raw!=0)>1}) }; } }