import type { InvestigationState } from "./stage1Contracts";
import type { EvidenceSourceRef } from "./stage1Contracts";
import type { LifecycleConfidence, LifecycleDefectType, LifecycleDomain, LifecycleResolution } from "./stage3Lifecycle";

export const CANDIDATE_EVIDENCE_SCHEMA_VERSION = "candidate_evidence_v0_1" as const;
export const PROBLEM_UNIT_SCHEMA_VERSION = "problem_unit_v0_1" as const;
export const PROBLEM_UNIT_SUMMARY_SCHEMA_VERSION = "problem_unit_summary_v0_1" as const;
export const INVESTIGATION_MAX_ACTIVE_PROBLEM_UNITS = 8;
export const INVESTIGATION_MAX_RESOLVED_PROBLEM_UNITS = 16;
export const INVESTIGATION_MAX_PROBLEM_UNIT_BACKLINKS = 12;
export const INVESTIGATION_MAX_FOCUS_PROBLEM_TYPES = 6;

export type ProblemUnitType =
  | "document_conflict"
  | "broken_chain_segment"
  | "lifecycle_anomaly_node"
  | "unresolved_settlement_cluster"
  | "period_risk_cluster"
  | "cross_branch_inconsistency_cluster";

export type ProblemSeverityGrade = "low" | "medium" | "high";
export type ProblemConfidenceGrade = "low" | "medium" | "high";

export interface ProblemScore {
  score: number;
  grade: ProblemSeverityGrade;
}

export interface ProblemConfidence {
  score: number;
  grade: ProblemConfidenceGrade;
}

export interface ProblemUnitEntityBacklink {
  entity: string;
  id: string;
}

export interface CandidateEvidenceItem {
  schema_version: typeof CANDIDATE_EVIDENCE_SCHEMA_VERSION;
  candidate_id: string;
  route: string;
  source_ref: EvidenceSourceRef;
  expected_state?: string;
  actual_state?: string;
  relation_pattern_hits: string[];
  anomaly_patterns: string[];
  entity_backlinks: ProblemUnitEntityBacklink[];
  confidence_hint: "high" | "medium" | "low";
}

export interface ProblemUnitPeriodImpact {
  is_period_sensitive: boolean;
  impact_class: "close_risk" | "reporting_risk" | "none";
}

export interface ProblemUnit {
  schema_version: typeof PROBLEM_UNIT_SCHEMA_VERSION;
  problem_unit_id: string;
  problem_unit_type: ProblemUnitType;
  title: string;
  mechanism_summary: string;
  business_defect_class: string;
  severity: ProblemScore;
  confidence: ProblemConfidence;
  affected_entities: string[];
  affected_documents: string[];
  affected_postings: string[];
  affected_accounts: string[];
  affected_counterparties: string[];
  affected_contracts: string[];
  expected_state?: string;
  actual_state?: string;
  failed_expected_edge?: string;
  period_impact?: ProblemUnitPeriodImpact;
  evidence_pack: string[];
  entity_backlinks: ProblemUnitEntityBacklink[];
  snapshot_limitations: string[];
  lifecycle_domain?: LifecycleDomain;
  lifecycle_object_id?: string;
  current_lifecycle_state?: string;
  expected_lifecycle_state?: string;
  missing_transition?: string;
  invalid_transition?: string;
  lifecycle_defect_type?: LifecycleDefectType;
  stale_duration?: string;
  lifecycle_confidence?: LifecycleConfidence;
  business_lifecycle_interpretation?: string;
  lifecycle_resolution?: LifecycleResolution;
  lifecycle_ranking_score?: number;
  lifecycle_ranking_basis?: string[];
}

export interface ProblemUnitSummary {
  schema_version: typeof PROBLEM_UNIT_SUMMARY_SCHEMA_VERSION;
  units_total: number;
  duplicate_collapses: number;
  unit_types: ProblemUnitType[];
  type_distribution: Partial<Record<ProblemUnitType, number>>;
  severity_distribution: Record<ProblemSeverityGrade, number>;
  confidence_distribution: Record<ProblemConfidenceGrade, number>;
  primary_unit_type: ProblemUnitType | null;
  lifecycle_enriched_units?: number;
  lifecycle_domain_distribution?: Partial<Record<LifecycleDomain, number>>;
  lifecycle_defect_distribution?: Partial<Record<LifecycleDefectType, number>>;
}

export interface InvestigationProblemUnitState {
  active_problem_units: string[];
  resolved_problem_units: string[];
  problem_unit_backlinks: Array<{
    problem_unit_id: string;
    entity_backlinks: ProblemUnitEntityBacklink[];
  }>;
  focus_problem_types: ProblemUnitType[];
}

export type InvestigationStateWithProblemUnits = InvestigationState & {
  problem_unit_state?: InvestigationProblemUnitState;
};
